Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of url path in form_for?

A copy action and copy.html.erb are defined in our rails 3.2 app. This copy action is to copy from the current record, allowing user modify slightly and submit for creation just like new. Here is the header of the copy.html.erb:

<%= form_for @engine_config, :as => :engine_config, :url => engine_configs_path do |f| %>

After clicking save, it hits create in controller and this is what we wanted.

Our question here is what the engine_configs_path stands for? Usually engine_configs_path is for index. Here the form is for create and is not index. What's the reasoning of this index path on create form?

like image 638
user938363 Avatar asked Dec 25 '22 07:12

user938363


2 Answers

When you do bundle exec rake routes you will see something similar to this;

engine_configs GET        /engine_configs(.:format)                               engine_configs#index
               POST       /engine_configs(.:format)                               engine_configs#create

Which means engine_configs_path works with both GET for index controller action and POST for create controller action.

Therefore engine_configs_path refers to the url the form is posted to in the controller.

like image 103
acacia Avatar answered Jan 08 '23 14:01

acacia


Form

You generally don't need to define the url argument in your form - Rails' form_for helper takes an initialized class object & extracts the required data for it automatically:

From the docs:

Typically, a form designed to create or update a resource reflects the identity of the resource in several ways:

  • (i) the url that the form is sent to (the form element's action attribute) should result in a request being routed to the appropriate controller action (with the appropriate :id parameter in the case of an existing resource)

  • (ii) input fields should be named in such a way that in the controller their values appear in the appropriate places within the params hash,

  • (iii) for an existing record, when the form is initially displayed, input fields corresponding to attributes of the resource should show the current values of those attributes.

In Rails, this is usually achieved by creating the form using form_for and a number of related helper methods. form_for generates an appropriate form tag and yields a form builder object that knows the model the form is about. Input fields are created by calling methods defined on the form builder, which means they are able to generate the appropriate names and default values corresponding to the model attributes, as well as convenient IDs, etc. Conventions in the generated field names allow controllers to receive form data nicely structured in params with no effort on your side.

--

Route

Your specific question is regarding the route your form_for method will use

As per w3c standards, HTML forms are defaulted to POST data. This means whenever you use a form_for helper in Rails, it will automatically use the POST variant of your routes

As described by Acacia, if you're using resourceful routes, you'll get a series of routes like this:

enter image description here

This means you can call the same route (engine_configs_path) with the HTTP Verb of POST to send to a specific controller#action, which is what's happening for you

like image 31
Richard Peck Avatar answered Jan 08 '23 15:01

Richard Peck