Abeid Ahmed
Abeid Ahmed

Reputation: 355

Is there a way to write a HTML form in rails?

I'm new to RoR and I've stumbled upon a problem. I'm trying to approve or deny a rent, and have been successful in doing so using the HTML form.

<form action="<%= approve_rental_url(rental) %>" method="post">
  <input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
  <input type="submit" value="Approve">
</form>

This code works without a problem and I know that it isn't the rails way of doing it. I've tried to write it in the rails way and have encountered a problem which says No route matches [POST] "/cats/2".

#Ruby code

<%= form_for approve_rental_url(rental) do |f| %>
  <%= f.submit "Approve" %>
<% end %>

Here is the rails routes

        Prefix Verb   URI Pattern                       Controller#Action
approve_rental POST   /rentals/:id/approve(.:format)    rentals#approve                                                   
   deny_rental POST   /rentals/:id/deny(.:format)       rentals#deny                                                     
       rentals POST   /rentals(.:format)                rentals#create                                                       
    new_rental GET    /rentals/new(.:format)            rentals#new                                                       
          cats GET    /cats(.:format)                   cats#index                                                      
               POST   /cats(.:format)                   cats#create                                                       
       new_cat GET    /cats/new(.:format)               cats#new                                                       
      edit_cat GET    /cats/:id/edit(.:format)          cats#edit                                                       
           cat GET    /cats/:id(.:format)               cats#show                                                       
               PATCH  /cats/:id(.:format)               cats#update                                                       
               PUT    /cats/:id(.:format)               cats#update                                                       
          root GET    /                                 cats#index                                                       

Upvotes: 0

Views: 51

Answers (2)

nathanvda
nathanvda

Reputation: 50057

Actually, since this is such a common problem, in rails you can just write

link_to 'Approve', approve_rental_url(rental), class: 'btn btn-success', method: :post

or use the button_to helper

button_to 'Approve', approve_rental_url(rental), class: 'btn btn-success'

(which will POST by default) and both will automatically inline a form to perform the POST.

Upvotes: 0

mechnicov
mechnicov

Reputation: 15248

When you use form_for, you have to pass record as argument like this:

<%= form_for :person do |f| %>
  First name: <%= f.text_field :first_name %><br />
  Last name : <%= f.text_field :last_name %><br />
  Biography : <%= f.text_area :biography %><br />
  Admin?    : <%= f.check_box :admin %><br />
  <%= f.submit %>
<% end %>

If you need to pass URL, you need to use form_tag instead of form_for:

form_tag('/posts/1', method: :put)

But these helpers are softly deprecated. Now there is form_with helper.

You can pass to it URL or record

form_with(model: nil, scope: nil, url: nil, format: nil, **options)

Upvotes: 2

Related Questions