处理多对多关联嵌套表单时遇到问题
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话(嵌套表单总是让我头疼),它应该看起来更好:
模型
控制器
查看
可用的详细信息此处。还有一个 Railscasts 示例
If i'm not wrong (nested forms always give me headaches) it should better look like that :
model
controller
view
detailed information available here. There is also a Railscasts example