Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to create user sessions -- > No route matches {:action=>"show", :controller=>"users", :id=>nil}
    primarykey
    data
    text
    <p>after finishing Michael Hart's tutorial on rails, I'm giving making a simple app of my own a try. </p> <p>I managed to create the users resource, and am now trying to create a sessions resource, to track when someone is logged in. I'm getting some really wacky results.</p> <p>1) I was trying to display different menu options for when someone is logged in, and for when someone is logged out. It works, except it behaves the opposite to the way I expected it ! i.e when I write </p> <pre><code> &lt;ul class="nav pull-right"&gt; &lt;% if signed_in? %&gt; #menu for registered users &lt;% else %&gt; #menu for unregistered users &lt;% end %&gt; &lt;/ul&gt; </code></pre> <p>When I'm logged in, the #menu for unregistered users shows up, and when I'm logged out, the #menu for registered users shows up. </p> <p>2) I'm also trying to create a link to the user's admin page, which is the basically the user's id page. </p> <p>I put this as the following as the link code</p> <pre><code>&lt;%= link_to "Admin", user_path(current_user) %&gt; </code></pre> <p>And got the error</p> <blockquote> <p>No route matches {:action=>"show", :controller=>"users", :id=>nil}</p> </blockquote> <p>This makes me think that the current_user I defined in the sessions helper is not being saved, but than I think, but I'm not sure why. </p> <p>Here's the code I have for the <strong>User Model</strong> </p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation has_secure_password before_save { |user| user.email = email.downcase } before_save :create_remember_token validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :password, presence: true, length: { minimum: 6 } validates :password_confirmation, presence: true private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 end end </code></pre> <p><strong>Sessions Controller</strong></p> <pre><code>class SessionsController &lt; ApplicationController def new end def create user = User.find_by_email(params[:session][:email].downcase) if user &amp;&amp; user.authenticate(params[:session][:password]) sign_in user redirect_to user else flash.now[:error] = 'Invalid Email/Password Combination' #Not quite right; render 'new' end end </code></pre> <p>end</p> <p><strong>SessionsHelper</strong></p> <pre><code>module SessionsHelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end def signed_in? !current_user.nil? end def current_user=(user) @current_user = user end def current_user @current_user ||= User.find_by_remember_token(cookies[:remember_token]) end end </code></pre> <p><strong>ApplicationController</strong></p> <pre><code>class ApplicationController &lt; ActionController::Base protect_from_forgery include SessionsHelper end </code></pre> <p>Thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload