![](/img/trans.png)
[英]How to update an attribute ':what_cause' of model user using group of radio button in rails3
[英]Rails 3: Using a submit button to update a model attribute
我有一個用戶模型,其中包含帶有布爾值的“電子郵件切換”列。 我想在我的視圖中創建一個按鈕,該按鈕允許用戶“打開”和“關閉”他們的電子郵件。 我無法使用“提交”按鈕來更新“用戶”模型中的值。
<%= form_for :user do |f| %>
<label>On</label>
<%= f.radio_button :email_switch, true %>
<label>Off</label>
<%= f.radio_button :email_switch, false %>
<%= f.submit "Save", :controller => "dashboard_emails", :action => "update", :method => "put" %>
<% end %>
class DashboardEmailsController < ApplicationController
before_filter :require_user
def index
end
def update
end
private
def require_user
@user = @logged_in_user
end
class User
field :email_switch, type: Boolean, default: false
end
您需要將參數傳遞給form_for
而不要傳遞給f.submit
調用。 如果您將持久用戶分配給@user
,則應該能夠執行以下操作:
<%= form_for @user do |f| %>
<label>On</label>
<%= f.radio_button :email_switch, true %>
<label>Off</label>
<%= f.radio_button :email_switch, false %>
<%= f.submit "Save" %>
<% end %>
當然,您需要config/routes.rb
resources :users
來config/routes.rb
起作用。 然后,這應將PUT請求發送到/users/47
,這將觸發UsersController
的#update
動作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.