1

I'm new to Ruby on Rails as well as Web development and was following the rails tutorial on youtube by Michael Hartl when I encountered a very confusing code. the code is:

def create
@user = User.new(params[:user])
end

I just can't understand where the ":user" key comes from or what it's value is. I've been trying to read all about symbols and hashes in ruby but it just confused me more. At first there was this code in the tutorial:

def show
@user = User.find(params[:id])
@title = @user.name
end

in which I understand that the "params[:id]" is a hash with key value :id, where :id => (id in the database) but with the params[:user], I just got lost. I don't have any column for "user", but my model is named "user".

So with that, my simple question would be... where did the key ":user" comes from and what is it's value?

1

3 Answers 3

1

It's from your form like

= form_for(@user, :url =>  url) do |f|
  = render 'shared/error_explanation', :object => @user

  = f.label t('users.email')
  = f.text_field :email, :autocomplete => 'off'

  = f.label t('users.password')
  = f.password_field :password, :autocomplete => 'off'

  = f.label t('users.password_confirmation')
  = f.password_field :password_confirmation, :autocomplete => 'off'

  %br

  = f.submit :class => 'btn'

It generates something like

<form accept-charset="UTF-8" action="/users/create_by_admin" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="xxx=" /></div>  
  <label for="user_Email">Email</label>
  <input autocomplete="off" id="user_email" name="user[email]" size="30" type="text" value="" />

  <label for="user_password">password</label>
  <input autocomplete="off" id="user_password" name="user[password]" size="30" type="password" />

  <label for="user_password confirmation">password confirmation</label>
  <input autocomplete="off" id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" />

  <br>

  <input class="btn" name="commit" type="submit" value="Create user" />
</form>

Look at name attributes. So params will be like {"utf8"=>"✓", "authenticity_token"=>"xxx=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create user"}

And User.creae methods get hash with atributes of the model.

Sign up to request clarification or add additional context in comments.

1 Comment

I see. Just to clarify if my understanding is correct, so in short, the value of the "params[:user] is the form?
0

Check the development logs after the form post. The params sending as

'user' => {'id' => 'someID', 'name' => 'someName', 'email' =>'some email' }

So there should be a user Hash containing all the field data. You can only capture the values in params[:user] in controller as though :user is the parent Hash.

Comments

0

To create new user we will do like below.

  <%= form_for :user do |f| %>
    ......
  <% end %>

When you call form_for, you pass it an identifying object for this form. In this case, it's the symbol :user.

In controller side we will do like below to create user record in data base

def create
@user = User.new(params[:user])
end

Through params[:user].inspect you can see what is coming to view to controller.

The params method is the object which represents the parameters (or fields) coming in from the form. The params method returns an ActiveSupport::HashWithIndifferentAccess object, which allows you to access the keys of the hash using either strings or symbols.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.