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?
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With