Simple_form 个问题
Simple_form issues
我对 simple_form
的关联有疑问。
这是模型"linked"
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 %>
但我有一个 collection 不包含 "on-going"、"called"、"not-contacted" 的列表,但我有一个列表:
State:0x00007fcb3afcfca8
如何显示我想要的列表?
你需要改成下面的样子
<%= f.association :state, collection: State.order(:category), label_method: :category, value_method: :id,prompt: "Choisir un état" %>
我对 simple_form
的关联有疑问。
这是模型"linked"
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 %>
但我有一个 collection 不包含 "on-going"、"called"、"not-contacted" 的列表,但我有一个列表:
State:0x00007fcb3afcfca8
如何显示我想要的列表?
你需要改成下面的样子
<%= f.association :state, collection: State.order(:category), label_method: :category, value_method: :id,prompt: "Choisir un état" %>