1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.