【发布时间】:2018-10-11 17:37:25
【问题描述】:
我对@987654321@ 的关联有疑问。
这里是“链接”的模型
class Contact < ApplicationRecord
belongs_to :state
end
class State < ApplicationRecord
CATEGORIES = [ "not-contacted", "on-going", "called"]
has_many :contacts
validates :category, inclusion: { in: CATEGORIES }
end
我还有一个State 种子,具有上述三个状态。
我正在为联系人创建新表单。
控制器:
def new
@contact = Contact.new
@states = State.all
end
def create
@contact = Contact.new(contact_params)
if @contact.save
redirect_to contact_path(params[:id])
else
render :new
end
end
查看:
<%= simple_form_for(@contact) do |f| %>
<%= f.input :last_name, label: 'Nom' %>
<%= f.input :first_name, label: 'Prénom' %>
<%= f.input :number, label: 'Téléphone' %>
<%= f.input :email, label: 'Mail' %>
<%= f.input :address, label: 'Adresse' %>
<%= f.association :state, collection: State.order(:category), prompt: "Choisir un état" %>
<%= f.input :grade, label: 'Note', collection:[1, 2, 3, 4, 5] %>
<%= f.submit 'Créer le contact', class:'btn-modifications' %>
<% end %>
但我有一个集合没有“正在进行”、“呼叫”、“未联系”的列表,但我有一个列表:
State:0x00007fcb3afcfca8
如何显示我想要的列表?
【问题讨论】:
-
试试这个
<%= f.association :state, collection: State.order(:category), label_method: :category, value_method: :id,prompt: "Choisir un état" %> -
@Pavan 谢谢,它有效!
标签: activerecord ruby-on-rails-5 simple-form