处理多对多关联嵌套表单时遇到问题

发布于 2024-12-08 09:59:46 字数 1623 浏览 0 评论 0原文

我从 dealcontroller 收到此错误 ActiveRecord::unknown attribute: store,我很确定与此行有关 @[电子邮件受保护](params[:deal])< /code> 这对于嵌套形式正确吗?这就是我的 有。

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

I am getting this error ActiveRecord::unknown attribute: store from dealcontroller,I am pretty sure has something to do with this line
@[email protected](params[:deal]) is this correct for nested form?This is what i have.

   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

deal model

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

store model

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

store-deal model

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

    end

city model

     class City < ActiveRecord::Base
 has_many :deals 
     end

view

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2024-12-15 09:59:46

如果我没记错的话(嵌套表单总是让我头疼),它应该看起来更好:

模型

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

可用的详细信息此处。还有一个 Railscasts 示例

If i'm not wrong (nested forms always give me headaches) it should better look like that :

model

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

controller

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

view

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

detailed information available here. There is also a Railscasts example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文