2016-01-20 75 views
0

所以,我有兩個型號:創建兩個的has_many belongs_to的關聯孩子和家長的模型在同一視圖

#app/models/diy.rb 
class Diy < Activerecord::Base 
    #schema id | summary | created_at | updated_at 
    has_many :steps 
end 

#app/models/step.rb 
class Step < ActiveRecord::Base 
    # schema id | step_content | photo | created_at | updated_at 
    belongs_to :diy 
end 

有什麼辦法來創建一個DIY的數據庫行和與之相關聯在同一視圖步驟數據庫行?

最近我得到的是:

<%= form_for(@diy) do |f| %> 
    <%= f.label :summary %><br> 
    <%= f.text_field :summary %><br> 
    <%= f.label :steps %><br> 
    <%= f.text_field :steps %><br> 
    <%= f.submit %> 
<% end %> 

但與此代碼我不是在步驟訪問表中的任何列。

如果它有助於解決問題,使用此代碼我會得到已填充「Step :: ActiveRecord_Associations_CollectionProxy:0x9613ce0」的「Steps」文本字段。

回答

3
class Diy < ActiveRecord::Base 
    has_many :steps 
    accepts_nested_attributes_for :steps 
end 

class Step < ActiveRecord::Base 
    belongs_to :diy 
end 

accepts_nested_attributes_for讓有關步驟自己動手取屬性:

Diy.create(steps_attributes: [{ step_content: 'Stir it.' }]) 

要創建表單輸入使用fields_for

<%= form_for(@diy) do |f| %> 
    <%= f.label :summary %><br> 
    <%= f.text_field :summary %><br> 
    <%- # wrapping it in a fieldset element is optional -%> 
    <fieldset> 
    <legend>Steps</legend> 
    <% f.fields_for(:steps) do |step_fields| %> 
     <%= step_fields.label :step_content %><br> 
     <%= step_fields.text_field :step_content %><br> 
    <% end %> 
    </fieldset> 
    <%= f.submit %> 
<% end %> 

這並迭代它雖然@diy.steps什麼,併爲每一個<textarea name="diy[steps_attributes][][step_content]">step_fields是一個表格生成器,其可以被限制在特定的嵌套記錄。

注意,如果@diy.steps是零喜歡上了一個新的記錄,然後就沒有表單輸入。爲了解決這個,你需要種子備案:

class DiysController 

    # ... 
    def new 
    @diy = Diy.new 
    @diy.steps.new # creates a new step that the user can fill in. 
    end 

    def edit 
    @diy = Diy.find(params[:id]) 
    @diy.steps.new # creates a new step that the user can fill in. 
    end 
end 

要避免遇到一堆垃圾步驟中,您將使用reject_if選項:

class Diy < ActiveRecord::Base 
    has_many :steps 
    accepts_nested_attributes_for :steps, reject_if: :all_blank 
end 

到白名單中的控制器嵌套屬性使用數組包含允許的屬性:

def diy_params 
    params.require(:diy).permit(:summary, steps_attributes: [:step_content]) 
end 

請閱讀:

1

爲了增加@Max的答案,你需要使用以下內容:

#app/models/diy.rb 
class Diy < Activerecord::Base 
    #schema id | summary | created_at | updated_at 
    has_many :steps 
    accepts_nested_attributes_for :steps 
end 

#app/controllers/diys_controller.rb 
class DiysController < ApplicationController 
    def new 
     @diy = Diy.new 
     @diy.steps.build 
    end 

    def create 
     @diy = Diy.new diy_params 
     @diy.save 
    end 

    private 

    def diy_params 
     params.require(:diy).permit(steps_attributes: [:step_content]) 
    end 
end 

#app/views/diys/new.html.erb 
<%= form_for @diy do |f| %> 
    <%= f.fields_for :steps do |s| %> 
     <%= s.number_field :step_count %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

-

如果你想只是聯想新Diy的現有Step S,你要填充step_idscollection_singular_ids)屬性:

#controller 
def diy_params 
    params.require(:diy).permit(step_ids: []) 
end 

#app/views/diys/new.html.erb 
<%= form_for @diy do |f| %> 
    <%= f.collection_select :step_ids, Step.all, :id, :name %> 
    <%= f.submit %> 
<% end %> 
相關問題