开发者

Ruby On Rails 3 Tutorial Chap 8: User Controller implementation

开发者 https://www.devze.com 2023-04-13 05:19 出处:网络
I am in chapter 8 of the book, where we are trying to implement the signup functionality for the sample app with actions \"new\" and \"create\". Here is my questions about these 2 methods/actions,

I am in chapter 8 of the book, where we are trying to implement the signup functionality for the sample app with actions "new" and "create". Here is my questions about these 2 methods/actions,

The "new" action/method is defined as below in the User controller

class UsersController < ApplicationController
  .
  .
  .
  def new
    @user = User.new
    @title = "Sign up"
  end
end

here the @user is defined so that its is accessible in the form of the html page for signup. As soon as the user hits signup button the "create" action/method of the user controller gets called, the code for the create action/method is below,

class UsersController < ApplicationController
  .
  .
  .
  def create
    @user = User.new(params[:user])
    if @user.save
      # Handle a successful save.
    else
      @title = "Sign up"
      render 'new'
    end
  end
end

Here is my question,

why are we calling "User.new" twice once inside the "new'method/action and inside "create" method/action" ?

Thanks for the reply,

what if I implement the create method lik开发者_开发百科e the one below, I have removed the second call to new, Is this wrong. if so what is wrong ?

def create
if @user.save
  # Handle a successful save.
else
  @title = "Sign up"
  render 'new'
end

end"

Thanks


If you are using the form_for implementation on the erb view.
This uses the @user object to associate the fields with the objects attributes. This fields will be passed as key value pairs.

<%= form_for @user do |u| %>
  <%= f.text_field :name %><br />
  <%= f.text_field :age %><br />
  <%= f.submit %>
<% end %>

For this you create a blank user object in the new method.

In the create method you create the object with the params submitted. This helps you to create a User object directly from the parameters, and validate and save the object.

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

After the submission of the form, the request params are passed to with the key as user object attributes.


The first time User.new is called, you are creating a model in memory that is used to generate the fields to populate the new user view. That html page then gets returned to the client, and the server forgets all about it. When the client fills out the form and commits it back to the server the create method gets called on the controller. The first thing the controller does is make a new User model, and populate it with the parameters. Until then, nothing has been persisted which is why the new method gets called twice


The first new in the new action is needed to get an empty object, which later is used in the user form in the view, so that Rails form helpers can determine the form object and have something to get the information Rails needs to automatically set all the default values of the form (like the default url to your UserController). With this information the form and page are rendewred and Rails forgets about it. (If the model has default values for some attributes, those will be set too and would appear in the form)

Now you have the form in your browser, fill in the values and submit it. This submit is handled by the create action and here the second new creates an object and fills it with the values submitted from your form and now available in the params hash. This object will have values and the @user.save call will save them to the database if they pass validation (result true). If there are errors, like missing data in mandatory fields, the save will fail and the form from the 'new' view will be rendered again. This time with the data in the object that was created, so all valid data will be filled in the input fields.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号