2014-02-07 55 views
0

我想製作一個按鈕,它改變了Object的布爾值。如何將params放入此行?

這一行:

= button_to 'Subscribe Back', candidate_subscriber_path(subscriber), method: :patch 

這使得這個請求:

{"_method"=>"patch", "action"=>"update", "controller"=>"candidate/subscribers", "id"=>"2"} 

我需要它來發送這些屬性:

subscribed: true 

不知道往哪裏放他們。你有什麼線索嗎?

嘗試:

= button_to 'Subscribe Back', candidate_subscriber_path(subscriber, subscribed: true), method: :patch 
= button_to 'Subscribe Back', { controller: 'subscribers', action: 'update', id: subscriber.id, subscribed: false }, method: :patch 

夫婦的其他選項。

爲我的作品的唯一選項是:

= simple_form_for :subscriber, url: candidate_subscriber_path(subscriber), 
                html: { method: :patch } do |f| 
    = f.input :subscribed, as: :hidden, input_html: { value: '1' } 
    = f.button :submit, 'Subscribe Back' 
+0

看一看這一點;同樣的問題:http://stackoverflow.com/questions/5363153/rails3-pass-arbitrary-parameters-via-button-to – steakchaser

+0

是的,試過 – user3007832

+0

你可以在裏面添加「subscribed:true」= simple_form_for:subscriber,url: candidate_subscriber_path(用戶,訂閱:真) –

回答

1

,我會接近它是專門爲創建操作的方式。

添加到您的候選路線 -

patch "/resubscribe/:id", to: "candidate/subscribers#resub" 

然後在控制器

def resub 
    user = YourModel.find(params[:id]) 
    user.update_attributes(subscribed: true) 
end 

那麼就發送路徑,您的按鈕

+0

是的,打破了CRUD – user3007832

相關問題