2013-09-16 84 views
1

我沒有得到預期的結果,技能沒有得到保存。我想,讓每個人都有發言權初級技能只有1次嵌套表格有很多通過rails4

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 %> 

技能沒有任何東西被保存,有人可以解釋發生了什麼?

+0

在'rails s'輸出日誌中檢查不允許的參數 –

+0

我的意思是服務器日誌 –

+0

全部都被接受,我嘗試了上面的方式和我發佈的准許參數的原始方式 - 兩者結束相同,沒有sql被執行除了Person sql – user1320651

回答

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> 

我有一些東西質樸,參數被接受但沒有正確地分配到表單,也使用錯誤的使用:在允許的參數中的技能,而不是:entity_skills_attributes => [:skill_id,:position]

要與更新所有我需要做的就是

def edit 
    @entity_skills= @person.entity_skills 
end 

我將使用位置重新安排技能的情況下,任何人想知道;-)

終於解決了

0

你不能通過保存的has_many無需構建聯接REC ORDS。

Dave Shepard也許會檢出這個tutorial,它貫穿您需要完整示例的事情。

+0

我的上面的解決方案工作,謝謝你的提示,但我已經使用了上面的代碼和它的好。 – user1320651