概要 *政治家有很多隶属关系。 *我有一个创建政治家的表格(使用form_for) *我使用嵌套属性来创建该表单中的从属关系(使用fields_for)
我设法完成这项工作的唯一方法是禁用Class Affiliation中的验证要求。我不得不禁用存在:对于polititian_id是真的
也许问题在于我使用entry_params的方式? 也许我应该使用reject_if?
模型
class Polititian < ActiveRecord::Base
has_many :affiliations
has_many :groups, through: :affiliations
accepts_nested_attributes_for :affiliations
validates :first_name, presence: true
validates :last_name, presence: true
end
class Affiliation < ActiveRecord::Base
belongs_to :group
belongs_to :polititian
#validates :polititian_id, presence: true
validates :group_id, presence: true
end
查看
<%= form_for (@polititian), :html => {:multipart => true} do |f| %>
<%= f.label :first_name, "First name(*)"%>
<%= f.text_field :first_name, class: "form-control", placeholder: "First name" %>
<%= f.fields_for :affiliation do |g| %>
<%= g.label :group_id, "Partido político(*)"%>
<%= g.collection_select :group_id, Group.all, :id, :name, {}%>
<% end %>
<%= f.submit %>
<% end %>
POLITITIAN CONTROLLER STRONG PARAMS METHOD
def entry_params
params.require(:polititian).permit(:id, :first_name, :last_name, :political_position, :political_position_start_at, :political_position_end_at, :avatar, :remote_avatar_url, :affiliations_attributes => [:group_id, :polititian_id] )
end