w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Reason for adding an @user variable to the new action(def new @user = User.new end)

What happens in the line

def new 
  @user = User.new

is, rails is creating an instance variable called @user (ruby uses @ sign for instance variables) and assigns a new User model. This could be most of the time the User model in


and then it passes to your view #app/vies/users/new.html.erb.

and remember, initializing a new User object and assign in to @user is not same as assign nil value to the @user

Then comes your second question, Why its giving an error when you exclude it deliberately.

So the reason behind that is,

when u say @user = User.new and pass the @user variable to rails form_helper. (<%= form_for(@user) do |f| %>), so form_helper automatically dose some things for you,

If you get the html source for the page it will be like

<form action="/users" id="new_user" method="post"> </form>

it means rails form helper is creating the routes, post method (post) etc for you. from your line <%= form_for(@user) do |f| %>. As u may already understand now, if you just pass 'nilvia@user = nil, railsform_helper` cannot do all that processing stuff , thats why you are getting the above error.

read more about form helpers , HTH

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.