嵌套表单has_many通过rails 4

时间:2013-09-16 13:51:47

标签: ruby-on-rails ruby ruby-on-rails-4 nested-forms

我没有得到预期的结果,技能没有得到保存。我想让每个人只说一个主要技能。

DB

  # Table name: people
  #
  #  id          :integer          not null, primary key
  #  first_name  :string(255)
  #  last_name   :string(255)
  #  headline    :string(255)
  #  description :string(255)
  #  user_id     :integer
  #  created_at  :datetime
  #  updated_at  :datetime
  #

  # == Schema Information
  #
  # Table name: skills
  #
  #  id          :integer          not null, primary key
  #  title       :string(255)
  #  description :text
  #  created_at  :datetime
  #  updated_at  :datetime
  #

  # == Schema Information
  #
  # Table name: entity_skills
  #
  #  id          :integer          not null, primary key
  #  skill_id    :integer
  #  person_id   :integer
  #  created_at  :datetime
  #  updated_at  :datetime
  #

模型

class Person < ActiveRecord::Base
  has_many :entity_skills
  has_many :skills, through: :entity_skills, foreign_key: "person_id"
  accepts_nested_attributes_for :skills
end


class Skill < ActiveRecord::Base
 has_many :entity_skills
 has_many :people, through: :entity_skills, foreign_key: "person_id"
end

class EntitySkill < ActiveRecord::Base
  belongs_to :person
  belongs_to :skill
end

控制器

def new
  @person = Person.new
  @all_skills = Skill.all
  @entity_skills = @person.skills.build

end

def edit
  @all_skills = Skill.all

  @entity_skills = @person.entity_skills.build
end

def create
  @person = Person.new(person_params)

  respond_to do |format|
    if @person.save
      format.html { redirect_to @person, notice: 'Person was successfully created.' }
      format.json { render action: 'show', status: :created, location: @person }
    else
      format.html { render action: 'new' }
      format.json { render json: @person.errors, status: :unprocessable_entity }
    end
  end
end

 def person_params
  params.require(:person).permit(:first_name, :last_name, :headline, :description, :user_id, :employer_id, skills_attributes: [:id])
end

表格

<%= form_for(@person) do |f| %>
  ....
  ....
  <h2>Skills</h2>
    <%= f.fields_for @entity_skills do |es| %>
        <%= es.label "All Skills" %>
        <%= collection_select(:skills, :id, @all_skills, :id, :title) %>
    <% end %>

   <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

没有为这些技能保存任何东西,有人可以解释发生了什么吗?

2 个答案:

答案 0 :(得分:0)

DB

  # == Schema Information
  #
  # Table name: entity_skills
  #
  #  id          :integer          not null, primary key
  #  skill_id    :integer
  #  person_id   :integer
  #  created_at  :datetime
  #  updated_at  :datetime
  #  position    :integer
  #

控制器

  def new
    @person = Person.new
    @all_skills = Skill.all
    5.times {@person.entity_skills.build}
  end

  def create
    @person = Person.new(person_params)
    respond_to do |format|
      if @person.save

        format.html { redirect_to @person, notice: 'Person was successfully created.' }
        format.json { render action: 'show', status: :created, location: @person }
      else
        format.html { render action: 'new' }
        format.json { render json: @person.errors, status: :unprocessable_entity }
      end
    end
  end

    def person_params
       params.require(:person).permit(:first_name, :last_name, :headline, :description, :user_id, :employer_id,  :entity_skills_attributes  => [:skill_id, :position, :person_id, :id])
    end

表格

  <h2>Skills</h2>
  <% i=0 %>
  <%= f.fields_for :entity_skills do |builder| %>
    <% i+=1 %>
    <%= render 'shared/skills_fields', :f => builder, :i=> i %>
  <% end %>

共享/ skills_fields

   <div class="field">
   <%= f.label "All Skills" %>

   <%= f.collection_select(:skill_id, Skill.all, :id, :title) %>
     <%= f.hidden_field :position, :value=>i %>
     <%= f.hidden_field :person_id, :value=>@person.id %>
  </div>

我有一些时髦的东西,params被接受但没有正确分配给表单,也犯了错误:使用允许的参数中的技能而不是:entity_skills_attributes =&gt; [:skill_id,:position]

要处理更新,我所要做的就是

 def edit
   @entity_skills=  @person.entity_skills
 end

如果有人想知道,我会使用这个职位来重新排序技能; - )

终于解决了

答案 1 :(得分:0)

如果不构建连接记录,则无法保存has_many。

也许请查看Dave Shepard的这个tutorial,它完整地展示了你需要完成的事情。