cheshireoctopus
cheshireoctopus

Reputation: 1790

Rails button_to...how to render as a <button> element?

Using the this button_to helper:

<%= button_to "Add Beer", "/growlers/create", method: "get" %>

yields the following HTML:

<form action="/growlers/create" class="button_to" method="get">
<div>
<input type="submit" value="Add Beer" />
</div>
</form>

Which is exactly what is supposed to happen. However, as you can see, the HTML element is a <form>. I am wondering how to render this as a <button> for styling purposes (using Foundation).

I found this:

<%= link_to "<button>Add Beer</button>".html_safe, "/growlers/create", method: "get" %>

yields this HTML:

<a data-method="get" href="/growlers/create">
<button>Add Beer</button>
</a>

Which, though a <button>, doesn't seem perfect to me.

I am curious if there are any other solutions/workarounds to this issue.

Upvotes: 1

Views: 1696

Answers (2)

grouchomc
grouchomc

Reputation: 482

If you wrap your button label in the button_to statement:

<%= button_to "/growlers/create", method: "get" do %>
  Add Beer
<% end %>

It will render the wrapped text within a element inside the form. props to apidock: https://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to

Upvotes: 3

Related Questions