Question

    class UserSessionsController < ApplicationController
  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Successfully logged in."
      redirect_to root_path
    else
      render :action => 'new'
    end
  end

Am new to RoR, So long i have been working on tradition c/c++ so i have some basic doubts about object creation and stuff, In UserSessionsController there is two methods namely "new" and "create". In the "new" method an object for UserSession is created without any parameters and in "create" method again object is created with some parameter.

Initially i thought that the "new" method is redundant and removed it. But i recieved the following error

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

The code works fine if i include the "new" method. I couldn't see this method being called anywhere in the entire code. Am referring to following sample project

railscasts/160-authlogic

Kindly let me know how this object creation is happening.

Thanks.

Was it helpful?

Solution

new and create are part of CRUD.

new action is used to render the new view for the UserSessionsController. In new action you simply create an instance of UserSession model class with @user_session = UserSession.new. After this, new.html.***(* is template handler like erb, haml, etc) is rendered where you will enter details of UserSession object that you would like to be created. Upon submission of this form create action would be invoked.

In create action you collect the parameters passed from the new view with @user_session = UserSession.new(params[:user_session]) and when you say @user_session.save it actually creates a record in database table user_sessions

UPDATE

The new action is invoked when you click on the Login Link. Why is it invoked? Because you have defined the login_path in routes.rb

Since you are a beginner I would highly recommend you to:

  1. Read the Getting Started with Rails which will help you to understand the fundamentals of a Rails Application development.
  2. Then, I also recommend you to complete Learn Rails by Example By Michael Hartl.
  3. And finally, watch the Ruby on Rails Railscasts By Ryan Bates.

Although, you can search on Google and you will find many great resources for the Rails beginners but the above 3 are THE de facto ones.

OTHER TIPS

The 'new' action is generally used in combination with a user interface that will accept input from the user such as a form. It is not strictly necessary that the new action create a new UserSession object, but it is necessary if you want to use a "form_for" helper.

<% form_for @user_session do |f| %>

As you can see, if @article is not defined, this form will raise an error. The benefits of using form_for are that rails will automatically generated the correct params for you when you submit the form and send the form-data to the create action. For example:

<% form_for @user_session do |f| %>
  <%= f.label :user_session %><br />
  <%= f.text_field :user_session %>
  <p><%= f.submit "Submit" %></p>
<% end %> 

This form will create a param user_session[:user_session] when you submit the form. Now, when you call:

@user_session=UserSession.new(params[:user_session])

the @user_session object will have its user_session attribute automatically set to the value passed in by the form. This might seem trivial when there is only one attribute, but in a form with many attributes the ability to instantiated a new object and set all the attributes in one line is nice.

This functionality can be recreated by hand but the form_for helper does all the work for you.

In UserSessionsController the new and create methods refer to controller actions that correspond to particular RESTful HTTP requests (routes). In this case, a GET /user_sessions/new HTTP request would invoke UserSessionsController#new and a POST /user_sessions HTTP request would invoke UserSessionsController#create.

The new action renders the form (found at views/user_sessions/new.html.erb) for creating a new user session. That view expects you to provide a user session object as @user_session, which is accomplished in the controller's new action by the @user_session = UserSession.new statement. Without that line, the view is trying to render the form with a nil object reference, resulting in your error.

The create action handles the form submission that comes from new. It expects to see a hash of properties that are appropriate for a UserSession. UserSession.new is called with that hash of properties, creating a new UserSession populated with data from the submitted form. Calling save on the UserSession instance runs validations, which can potentially fail. You can see that if the save succeeds, the controller will redirect the user to the root URL with a "Success!" flash message. If it fails, it sends the user back to the form to fix their mistakes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top