0

我從dealcontroller得到這個錯誤ActiveRecord::unknown attribute: store,我很肯定與這條線有關 @[email protected](params[:deal])這是正確的嵌套形式嗎?這就是我所擁有的。多對多關聯嵌套表格遇到問題

class dealController < ApplicationController 
    def create 
    @city = City.find(session[:city_id]) 
    @[email protected](params[:deal]) 
    if @deal.save 
    flash[:notice]="successfully created" 
    redirect_to @deal 
    else 
    render 'new'  
    end 
    end 
    end 

交易模型

class Deal < ActiveRecord::Base 
belongs_to :city 
has_many :stores ,:through =>:store_deals 
has_many :store_deals 
accepts_nested_attributes_for :store_deals 
    end 

商店模式

class Store < ActiveRecord::Base 
has_many :deals ,:through =>:store_deals 
has_many :store_deals 
    end 

商店的交易模型

class StoreDeal < ActiveRecord::Base 
belongs_to :store 
belongs_to :deal 

    end 

城市模型

 class City < ActiveRecord::Base 
has_many :deals 
    end 

視圖

<%= form_for @deal ,:url=>{:action =>"create"} do |f|%> 

    <%= f.text_field :item_name %><br/> 

    <%=f.fields_for :store_deal do |s| %> 
    <%=s.text_field :store_name %> 
    <%end%> 
    <%= f.submit "post"%> 

<%end%> 

回答

1

,如果我沒有錯(嵌套形式總是給我頭痛),應該更好地看起來像:

模式

class Deal < ActiveRecord::Base 
    belongs_to :city 
    has_many :stores ,:through =>:store_deals 
    has_many :store_deals 
    accepts_nested_attributes_for :stores 
end 

控制器

def new 
    @city = City.find(session[:city_id]) 
    @deal = @city.deals.build 
    @deal.stores.build 
end 

def create 
    @city = City.find(session[:city_id]) 
    @deal = @city.deals.build(params[:deal]) 

    # etc. 
end 

視圖

<%= form_for @deal ,:url=>{:action =>"create"} do |f|%> 
    <%= f.text_field :item_name %><br/> 
    <%= f.fields_for :stores do |s| %> 
    <%= s.text_field :name %> 
    <% end %> 
    <%= f.submit "post" %> 
<% end %> 

詳細資料here。還有一個Railscasts example

+0

仍然沒有工作 – katie

+0

對不起,不知道爲什麼。還是一樣的錯誤信息? –