Rails button_to vs. HTML Button Tag

19,887

Solution 1

button_to like all others helpers for the views in Rails (e.g. link_to, form_for, image_tag...) simply converts your Rails command in HTML code. We could say that is a shortcut to write "pure HTML".

In your case, for button_to, you can see the reference (and what is printed) here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

For example:

<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
#      <div><input value="New" type="submit" /></div>
#    </form>"

I suggest you to use these helpers, because is more "Rails" and this improve your speed of coding.

Solution 2

button_to tag is as easy to customize as html tag.

There is a convention to not style HTML tags inside this elements but in included *.css files. All customization of your HTML tags should be addition of classes and ids.

To do it in rails you should use

<%= button_to "New", { action: "new" }, class: "my_class", id: "my_id" %>

It will generate the html code like this:

<form method="post" action="/controller/new" class="button_to">
  <div><input class="my_class" id="my_id" value="New" type="submit" /></div>
</form>"

As you see, using Rails helpers you can write the same functionality with less, more readable code.

Share:
19,887

Related videos on Youtube

dev
Author by

dev

Updated on June 04, 2022

Comments

  • dev
    dev almost 2 years

    I am new to Rails and I am trying to add a few buttons to my page. I see that you can use both the HTML tag or the rails "button_to." However, I have not been able to find out the difference in each.

    From what I have seen in S.O., it seems that button_to makes it easier to call functions from the controller, etc. However, HTML button seems easier to customize? Is this true? Are there other differences.

    Just looking to ensure I am doing the correct thing on my new project. Thanks in advance.

    • Sergio Tulentsev
      Sergio Tulentsev almost 10 years
      In the end, button_to will generate an html button (so it's just a convenience wrapper)
    • DMKE
      DMKE almost 10 years
      @CameronJones: Have you read the documentation on button_to?
  • dev
    dev almost 10 years
    Thanks. Great answer. Follow up question. If I didn't want to use button_to, how would I use the "onclick" property of Button to call a function in my controller?
  • damoiser
    damoiser almost 10 years
    @CameronJones If you don't want to use the button_to and using simply HTML, to call a specific action you could simply put the path for this action in the "action" attribute for the button (like my example code) or even easier using a link. E.g. <a href="path_to_my_controller/name_of_the_action">
  • damoiser
    damoiser almost 10 years
    more "Rails", you can use the helpers for the "path", like: <%= new_user_path %> (appending the path to the name) that print e.g.: "users/new". You can see all the routes in the routes.rb or calling this command in the terminal: "rake routes"