嘿,我一直在使用has_and_belongs_to_many關係與Railscast Episode #17複選框示例。我有一些問題,現在一切工作順利,除非更新按鈕不起作用。更新與HABTM關係複選框的值 - Rails
編輯視圖看起來像這樣
<% form_for :users, :action => 'update' do |f| %>
<% for interest in Interest.find(:all) %>
<div>
<%= check_box_tag "user[interest_ids][]", interest.id,
@user.interests.include?(interest) %>
<%= interest.name %>
</div>
<% end %>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
,並在控制器我....
def edit
@user = User.find(session[:user_id])
end
def update
params[:user][:interest_ids] ||= []
@user = User.find(session[:user_id])
if @user.update_attributes(params[:user])
flash[:notice]='User data was updated'
redirect_to :action => 'index'
else
redirect_to :action => 'index'
end
end
按鈕未事件做重定向......所以我不知道發生了什麼。在我的表單創建中有什麼搞砸了嗎?我不完全知道如何創建一個按鈕,並將其訪問方法與模型更新控制器等
我到處尋求幫助時,也許認爲這是因爲attr_accessible的,所以我說=>
attr_accessible :login, :email, :password, :password_confirmation, :interest_ids, :user
我的用戶模型,但仍然沒有任何...我的想法爲什麼我的表單不提交?
這並不處理您將它們全部取消選中時的情況。 – 2012-05-01 21:18:08
並沒有工作在最新版本的導軌 – 2012-10-18 17:29:14
當我們有所有未經檢查的導軌將刪除所有關係 – antiqe 2013-01-21 11:13:49