user4227507
user4227507

Reputation:

Ruby on Rails Convert Button To Link

I have this button in a view:

<%= button_to 'Add Review', {controller: 'reviews', action: 'new', id: booking.showing.film.id } %>

Which then sends the user to the new review path and uses the film's id to enable the new review form to know what film is being reviewed. This is done through the routes:

post 'reviews/new/:id', to: 'reviews#new'

But the button does not look very nice so I want a link_to. I have tried this:

<%= link_to 'Add Review', {controller: 'reviews', action: 'new', id: booking.showing.film.id } %>

But through my errors routing:

 match '*a', to: 'errors#routing', via: [:get, :post]

It will send the user back to the index page and display the message: No such path as 'reviews/new/6'

How can I make a link that does exactly the same as the button?

Upvotes: 1

Views: 446

Answers (2)

mgrim
mgrim

Reputation: 1302

You can use the :method option to set http verb (e.g. :post) in link_to (see http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to).

However, you should consider changing your route since the reviews#new action renders the new review form. Use the POST request when creating or updating data.

Upvotes: 0

S M
S M

Reputation: 8165

Your route requires a POST request, which the button makes, but link_to makes an <a> element, which will make a GET request. You can't make an <a> submit a POST. I suggest you use the button and restyle it with CSS to look like a link.

Upvotes: 1

Related Questions