0

What is the difference between this

<%= form_for(:user, url: users_path) do |f| %>  

and

<%= form_for @user do |f| %> 

Why is the users_path necessary and what is it doing?

Why in the first form is the :user symbol used and not @user?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Good example here http://stackoverflow.com/questions/10414514/rails-change-routing-of-submit-in-form-for and also docs here http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for – Richlewis Dec 18 '15 at 21:31

1 Answers1

1

In the first way --

<%= form_for(:user, url: users_path) do |f| %>  

you will get the exact same form html wise (for example <input name="user[age]" />)

BUT

When you provide a specific @user object (that you define in the controller) the fields will be filed with the information saved in the @user var.

for example, if I define a @user in the controller and say

@user.age = 5

and than I do a form_for and load put an input for a name it will already fill it with "gilad":

<input name="user[age]" value=5 />

Also, in the first type of form the :url is the url the form will be submitted to.

Hope this helped

user3926081
  • 49
  • 1
  • 1
  • 9