Rails - 连接复选框以更改连接表的值
我有两个模型事件和兴趣,它们通过连接表迁移事件兴趣(event_id,interest_id,id => false)连接。
我试图创建一些东西,您可以将一组兴趣类别连接到我的应用程序中的某些事件。
我直接在数据库中创建了一条记录,并且它显示正确,所以我很确定复选框部分是正确的;但我根本无法编辑记录。如果我通过复选框添加另一个兴趣,则不会发生任何事情。我没有收到任何错误,并且日志显示它调用连接表,所以我猜测问题出在我的更新语句上......
我认为它与attr_accessible 部分,但我不知道如何为连接表添加它......我只需删除它吗?
events.rbinterests.rb
has_and_belongs_to_many :interests
attr_accessible :name, :category
在我的编辑表单中
has_and_belongs_to_many :events
,我想将记录添加到连接表中,它看起来像这样。
<%= form_for @event do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<% for interest in @interests %>
<div>
<%= check_box_tag "event[interest_ids][]", interest.id, @event.interests.include?(interest) %>
<%= interest.name %>
</div>
<% end %>
<p><%= f.submit %></p>
控制器中的编辑/更新部分看起来像
def update
@event = Event.find(params[:id])
params[:event][:interest_ids] ||= []
if @event.update_attributes(params[:event])
flash[:notice] = "Successfully updated event."
redirect_to @event
else
render :action => 'edit'
end
end
I have two models Events and Interests which are connected by a join table migration EventsInterests (event_id, interest_id, id=>false).
I was trying to create something where you could connect a set of interest categories to some events within my app.
I created a record in directly in the database and its showing up correctly, so I'm pretty sure the checkbox part is correct; but I cant edit the the record at all. If I add another interest through the checkbox, nothing happens..Im not getting any errors and the log shows it calling the join table, so I'm guessing the issue is with my update statement.....
Im thinking its something with attr_accessible part but I dont know how to add that for a join table....do i just delete it?
events.rb
has_and_belongs_to_many :interests
attr_accessible :name, :category
interests.rb
has_and_belongs_to_many :events
In my edit form where I would like to add the records to the join table it looks like this.
<%= form_for @event do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<% for interest in @interests %>
<div>
<%= check_box_tag "event[interest_ids][]", interest.id, @event.interests.include?(interest) %>
<%= interest.name %>
</div>
<% end %>
<p><%= f.submit %></p>
and the edit/update part in the controller looks like
def update
@event = Event.find(params[:id])
params[:event][:interest_ids] ||= []
if @event.update_attributes(params[:event])
flash[:notice] = "Successfully updated event."
redirect_to @event
else
render :action => 'edit'
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想更新这样的模型,你应该使用 has_many :through 关系。
拥有和属于模型仅在创建/删除关系时才有意义。
请参阅 http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
If you want to update such models, you should use a has_many :through relation.
Has and belongs to models make only sense when only creating / deleting a relation..
See http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association