I’m relatively new to Ruby on Rails. As of now have this working code in one of the views in my Rails 2.3.8 application:
<% form_for(@configuration) do |f| %>
<%= f.text_field :parameter_a %>
<%= f.text_field :parameter_b %>
<%= button_to( "Apply", {} , { :method => "put" } ) %>
<% end %>
As expected pressing the “Apply” button calls update in the controller. Now I would like to have the button shown in a hyperlink style and I’m looking for most efficient way to do this.
Maybe there are other ways, but I didn’t figure out how to use link_to correctly. It never passed the updated values in the PUT request. If you’d recommend link_to, could you please provide some hints how to do it. This is the non-working code:
<%= link_to( "Apply", configuration_path( @configuration ), { :method => "put" } ) %>
Many thanks.
One way to do this is by using the html
buttonelement and some css. I much prefer this technique since it requires no JS. This can also be done on input tags, using either their type or their class to select them, but buttons are nicer 😉As an aside, you shouldn’t be using
button_tothere. Rails’button_totag is supposed to be used outside a form (because it builds it’s own form), you should usef.submit. It will generate an input with a type of submit. Or, you could use a button submit.Back to using a
buttontag, I prefer to create a helper:Then, once you’ve used that, you can make the button look like a link with this css:
You may need to use JS to get the hover state to work in some browsers, but it’s still better than using JS for the submission of the form.