รูปแบบที่ซ้อนกันมีมากมายผ่านราง4

ฉันไม่ได้รับผลลัพธ์ตามที่คาดหวัง ทักษะไม่ได้รับการบันทึก ผมอยากให้แต่ละคนได้พูดทักษะเบื้องต้นเท่านั้น 1.

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

ไม่มีการบันทึกไว้สำหรับทักษะนี้ ใครช่วยอธิบายหน่อยได้ไหมว่าเกิดอะไรขึ้น


person user1320651    schedule 16.09.2013    source แหล่งที่มา
comment
ตรวจสอบพารามิเตอร์ที่ไม่ได้รับอนุญาตในบันทึกเอาต์พุต rails s   -  person Oleg Haidul    schedule 16.09.2013
comment
ทั้งหมดได้รับการยอมรับแล้ว ฉันลองใช้วิธีข้างต้น และวิธีการเดิมที่ฉันโพสต์สำหรับพารามิเตอร์ที่ได้รับอนุญาต - ทั้งคู่จบลงเหมือนกัน ไม่มีการดำเนินการ sql นอกเหนือจาก Person sql   -  person user1320651    schedule 16.09.2013


คำตอบ (2)


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

แบ่งปัน/ทักษะ_ฟิลด์

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

ฉันมีบางสิ่งที่ขี้ขลาด พารามิเตอร์ได้รับการยอมรับแต่ไม่ได้กำหนดอย่างถูกต้องให้กับแบบฟอร์ม และยังทำผิดพลาดในการใช้ :skills ในพารามิเตอร์ที่ได้รับอนุญาตแทน :entity_skills_attributes => [:skill_id, :position]

ในการทำงานกับการอัปเดตทั้งหมดที่ฉันต้องทำคือ

 def edit
   @entity_skills=  @person.entity_skills
 end

ฉันจะใช้ตำแหน่งเพื่อเรียงลำดับทักษะใหม่ เผื่อใครสงสัย ;-)

ในที่สุดก็แก้ได้

person user1320651    schedule 16.09.2013

คุณไม่สามารถบันทึก has_many ผ่านโดยไม่ต้องสร้างบันทึกการรวม

บางทีลองดู บทแนะนำโดย Dave Shepard ซึ่งจะอธิบายสิ่งที่คุณต้องทำพร้อมตัวอย่างฉบับเต็ม

person Andrew Hacking    schedule 16.09.2013
comment
วิธีแก้ปัญหาข้างต้นของฉันใช้งานได้ ขอบคุณสำหรับคำแนะนำ ฉันใช้โค้ดด้านบนแล้วและก็โอเค - person user1320651; 16.09.2013