button_to with GET method option in Rails

╄→尐↘猪︶ㄣ 提交于 2019-12-04 17:04:11

问题


I have the following button, which I overwrited to generate a GET request:

= button_to "Tutor", {:controller => "appointments", :action => "new",
    :listing_id => @listing.id} , :method => :get

However, I still get a POST request with extra params :method:

  Processing by AppointmentsController#new as HTML
  Parameters: {"authenticity_token"=>"AWkL", "listing_id"=>"2", "method"=>"get"}

I my routes file, I have:

 resources :appointments

What did I do wrong?

Thank you.


回答1:


Buttons aren't supposed to be sending GET requests.

You should use a link_to instead. If you want it to look like a button, apply some CSS. Here's a good article: http://coding.smashingmagazine.com/2009/11/18/designing-css-buttons-techniques-and-resources/




回答2:


Just use method: and :get

button_to "New User", new_user_path, method: :get



回答3:


You may want to look at the UJS driver code:

a\[data-method\] is the selector used here:

So the following should work (couldn't really test it):

button_to "Tutor", {...}, :html => { "data-method" => "get" }


来源:https://stackoverflow.com/questions/8684467/button-to-with-get-method-option-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!