title: Create Session in Rails 3.1 Hartl Tutorial
body: <p>This question is admittedly long. So I appreciate any support from the rubytutorial community. I am in Chapter 9, attempting to create a session for a logged-in user. </p>
  
  <p>I've done the tutorial already in &lt; Rails 3.1. Since I am now using Rails 3.1, I headed on over to Chapter 13 and linked to the (very good) <a href="http://railscasts.com/episodes/270-authentication-in-rails-3-1" rel="nofollow">Railscasts (#270) on the subject</a>. I was able to rewrite my user sign up pretty easily thanks to has_secure_password. </p>
  
  <p>When I try to log in with a user in the database I see this in the console):</p>
  
  <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"users"}
  </code></pre>
  
  <p>Seems like I need to create a route and it should work. But if that is the case, why can I go 'users/1' and the view appear? I am using the route user_path(@user), @user in my sessions and users controllers (below).</p>
  
  <p>Here is what I did.</p>
  
  <p>Pass a form to the Session controller new action (note: I use form_tag and not form_for)</p>
  
  <pre><code>    &lt;%= form_tag sessions_path do %&gt;
  
      &lt;div class="field"&gt;
          &lt;%= label_tag :email %&gt;&lt;br /&gt;
          &lt;%= text_field_tag :email, params[:email] %&gt;
      &lt;/div&gt;
  
      &lt;div class="field"&gt;
          &lt;%= label_tag :password %&gt;&lt;br /&gt;
          &lt;%= password_field_tag :password %&gt;
      &lt;/div&gt;
  
      &lt;div class="actions"&gt;
          &lt;%= submit_tag "Sign In" %&gt;
      &lt;/div&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>Then, create action in sessions_controller.rb</p>
  
  <pre><code>def create
  #Assign object by email attribute
  user = User.find_by_email(params[:email])
  # User is present and has access, must be true otherwise nil object
  if user &amp;&amp; user.authenticate(params[:password])
      session[:user_id] = user.id
      RIGHT HERE IS THE PROBLEM
      redirect_to user_path(@user), :notice =&gt; "Logged in!"
  else
      #Use flash.now on render not flash[]
      flash.now.alert = "Invalid email or password"
      render "new"
  end
  end
  </code></pre>
  
  <p>And finally Create action for users_controller.rb, which works fine.</p>
  
  <pre><code>def create
   @user = User.new(params[:user])
      if @user.save
          flash[:success] = "Welcome to the Sample App!"
          redirect_to @user
      else
          render "new"
          @user.password.clear
          @user.password_confirmation.clear
      end
  end
  </code></pre>
  
  <p>Leaving my User model:</p>
  
  <pre><code>attr_accessible :name, :email, :password, :password_confirmation
  
  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  
  has_secure_password
  </code></pre>
  
  <p>Oh and here is my current routes.rb file</p>
  
  <pre><code> resources :users
   resources :sessions, only: [:create, :new, :destroy]
  
   root to: "pages#home"
   match "/about",        to: "pages#about"
   match "/contact", to: "pages#contact"
   match "/help",         to: "pages#help"
   match "/signup",   to: "users#new" 
   match "/signin",   to: "sessions#new"
   match "/signout",  to: "sessions#destroy"
  </code></pre>
  
  <p>And finally my output when I run rake routes:</p>
  
  <pre><code>users GET    /users(.:format)          {:action=&gt;"index", :controller=&gt;"users"}
  POST   /users(.:format)          {:action=&gt;"create", :controller=&gt;"users"}
  new_user GET    /users/new(.:format)      {:action=&gt;"new", :controller=&gt;"users"}
  edit_user GET    /users/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"users"}
  user GET    /users/:id(.:format)      {:action=&gt;"show", :controller=&gt;"users"}
  PUT    /users/:id(.:format)      {:action=&gt;"update", :controller=&gt;"users"}
  DELETE /users/:id(.:format)      {:action=&gt;"destroy", :controller=&gt;"users"}
  sessions POST   /sessions(.:format)       {:action=&gt;"create", :controller=&gt;"sessions"}
  new_session GET    /sessions/new(.:format)   {:action=&gt;"new", :controller=&gt;"sessions"}
  session DELETE /sessions/:id(.:format)   {:action=&gt;"destroy", :controller=&gt;"sessions"}
  root        /                         {:controller=&gt;"pages", :action=&gt;"home"}
  about        /about(.:format)          {:controller=&gt;"pages", :action=&gt;"about"}
  contact        /contact(.:format)        {:controller=&gt;"pages", :action=&gt;"contact"}
  help        /help(.:format)           {:controller=&gt;"pages", :action=&gt;"help"}
  signup        /signup(.:format)         {:controller=&gt;"users", :action=&gt;"new"}
  signin        /signin(.:format)         {:controller=&gt;"sessions", :action=&gt;"new"}
  signout        /signout(.:format)        {:controller=&gt;"sessions", :action=&gt;"destroy"}
  </code></pre>