While trying to follow a tutorial, I raised an error when I test the "Sign out" link. I checked the difference with the tutor, but I couldn't figure out why I can't do it my way and why the error occurs on this spot.
My code:
class SessionsController < ApplicationController
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
redirect_to(session[:intended_url] || user), notice:"Welcome back, #{user.name}"
session[:intended_url] = nil
else
flash.now[:alert] = "Invalid email/password combination! you are a failure"
render :new
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "You're now signed out!"
end
end
The correction code:
class SessionsController < ApplicationController
def new
end
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
flash[:notice] = "Welcome back, #{user.name}!"
redirect_to(session[:intended_url] || user)
session[:intended_url] = nil
else
flash.now[:alert] = "Invalid email/password combination!"
render :new
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: "You're now signed out!"
end
end
The error that was raised:
SyntaxError in SessionsController#destroy
C:/Users/xcpro/ve2/2B3/app/controllers/sessions_controller.rb:6:
syntax error, unexpected ',', expecting keyword_end
...ession[:intended_url] || user), notice:"Welcome back, #{user... ...
^
The problem is that method redirect_to take parameters only in parentheses, however notice also should passed in it. Add parentheses around all redirect_to parameters:
redirect_to( (session[:intended_url] || user), notice: "Welcome back, #{user.name}" )
or maybe space after redirect_to would work also:
redirect_to (session[:intended_url] || user), notice: "Welcome back, #{user.name}"
Related
Not sure how its possible to get this error :
AbstractController::DoubleRenderError users#create
When in my controller I got this code :
render 'new' and return
I got the log from the bugsnag saying that I got the error at this line.
This is the create method code :
def create
back_button and return if params[:back_button]
#profile = current_user.build_profile(params[:user])
if #profile.nil? || current_user.nil? || #profile.user.nil?
sign_out
redirect_to signup_path and return
end
if #profile.new_record?
render 'new' and return
else
redirect_to more_questions_path and return
end
end
I have before filter in this controller :
before_filter :signed_in_user
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
Give this a try:
class UsersController < ApplicationController
before_filter :signed_in_user
def create
return back_button if params[:back_button]
#profile = current_user.build_profile(params[:user])
if #profile.nil? || current_user.nil? || #profile.user.nil?
sign_out
return redirect_to signup_path
end
if #profile.new_record?
render 'new'
else
redirect_to more_questions_path
end
end
private
def signed_in_user
unless signed_in?
store_location
return redirect_to signin_url, notice: "Please sign in."
end
end
end
The reasoning behind it: x and return means x and return nil, thus returns nil. Actually, you try to short-circuit the controller action, and return redirect_to ....
The and isn't doing anything for you.
In each place where you have xxx and return, replace it with
xxx
return
For example:
redirect_to signup_path
return
That should work more like you would expect it to.
You have a render and a redirect. You have to pick one.
I suppose redirect_to signup_path is returning either nil or false, thus your and return is not being executed.
You can fix this many ways, the simplest is replace
redirect_to signup_path and return
by
redirect_to signup_path
return
Yet, I suggest you to do a bigger change. Try changing this
if #profile.nil? || current_user.nil? || #profile.user.nil?
sign_out
redirect_to signup_path and return
end
if #profile.new_record?
render 'new' and return
else
redirect_to more_questions_path and return
end
By
if #profile.nil? || current_user.nil? || #profile.user.nil?
sign_out
redirect_to signup_path
elsif #profile.new_record?
render 'new'
else
redirect_to more_questions_path
end
This way it is clear that only one path can be taken, without relying on return.
I have been stuck on this error for a while now, I've looked around and apparently it is due to too many end 's, but I can't seem to see why, any help guys? thanks.
Here's my code
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end # your missed end here
end
end # another end missed for the class
My question is actually fairly simple, how do I make a create action which checks if a user is logged in, and if she/he is then redirect to the dashboard instead of rendering the index page where they've got links and stuff to go to and sign up. Also why is the code below not working.
class UsersController < ApplicationController
def new
#user = User.new
end
def create
if current_user.nil?
redirect_to dplace_index_path
if current_user
#user = User.new(params[:user])
if #user.save
auto_login(#user)
redirect_to dplace_index_path
end
end
end
end
end
Your code isn't doing what you expect because the if statements are actually nested (you want elsif with this same structure -- or see my suggested fix below). Here's what your code, when properly formatted, actually looks like:
def create
if current_user.nil?
redirect_to dplace_index_path
if current_user
#user = User.new(params[:user])
if #user.save
auto_login(#user)
redirect_to dplace_index_path
end
end
end
end
Logically, you will never get down into the second if statement, because current_user must be nil to enter the first. Try something like this instead:
def create
if current_user
#user = User.new(params[:user])
if #user.save
auto_login(#user)
redirect_to dplace_index_path
end
else
redirect_to dplace_index_path
end
end
I rearranged the code, but it should logically do what you want now. I put the "happy path" first (the current_user exists), and moved the redirect into the else statement.
General user authentication:
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to dashboard_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
Try:
def create
if current_user.blank? # .blank? will check both blank and nil
# logic when user is not logged in
redirect_to index_path
else
# logic when user is logged in
redirect_to dashboard_path
end
end
def create
redirect_to dplace_index_path unless current_user
# no need to check current_user again
#user = User.new(params[:user])
if #user.save
auto_login(#user)
redirect_to dplace_index_path
end
end
I everyone, I've some issue to handling exception in ruby. I doesn't understand why my statement doesn't work.
Error : Couldn't find User with id=14
I want to redirect to the login page.
def login_required
begin
if session[:user_id] == nil
redirect_to login_path, :notice => "You are not logged"
elsif User.find(session[:user_id])
return nil
end
rescue ActiveRecord::RecordNotFound
redirect_to login_path, :notice => "No user corresponding in database"
end
end
Hope you can help me.
Cordially,
Aubin
def login_required
begin
if session[:user_id] == nil
redirect_to login_path, :notice => "You are not logged"
elsif User.find_by_id(session[:user_id]).nil?
#rescue ActiveRecord::RecordNotFound (use if u want to use User.find)
redirect_to login_path, :notice => "No user corresponding in database"
return nil
end
end
end
The only reason for this to not work is that ActiveRecord is actually finding a row
User.find(session[:user_id])
Try logging the session[:user_id] and looking into DB using SQL.
On another note, you can use
session[:user_id].nil?
instead of
session[:user_id] == nil
I would rewrite your method as follows:
def login_required
return redirect_to(login_path,
:notice => "You are not logged") if session[:user_id].blank?
user = User.find_by_id(session[:user_id])
return user if user.present?
redirect_to login_path, :notice => "No user corresponding in database"
end
I have authlogic and openid working correctly, and am trying to integrate oauth for twitter authentication. I do not want both a register and sign on button, so I have followed this example: Implicit user creation with Authlogic and Authlogic OAuth plugin
I get the DoubleRenderError on initial registration and subsequent log in, but refreshing the page lets the action complete successfully.
My users_controller create:
def create
#user = User.new(params[:user])
#user.save do |result| # LINE A
if result
flash[:notice] = "Account registered!"
redirect_to account_url
else
unless #user.oauth_token.nil?
#user = User.find_by_oauth_token(#user.oauth_token)
unless #user.nil?
UserSession.create(#user)
flash.now[:message] = "Welcome back!"
redirect_to account_url
else
redirect_back_or_default root_path
end
else
redirect_back_or_default root_path
end
end
end
end
And my user_sessions_controller create:
def create
#user_session = UserSession.new(params[:user_session])
#user_session.save do |result|
if result
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end
end
Is there a way to resolve this? Thanks
After repeated failures, the following appears to work for normal authlogic username/password, OAuth with Twitter, and OpenID for at least google and yahoo, which is all I was interested in
def create
#user = User.new(params[:user])
#user.save do |result| # LINE A
if result
flash[:notice] = "Account registered!"
redirect_to account_url and return
else
if #user.oauth_token
#user = User.find_by_oauth_token(#user.oauth_token)
UserSession.create(#user)
flash.now[:message] = "Welcome back!"
redirect_to account_url and return
else
flash[:notice] = "Something went awry. Perhaps the name or email is already in use."
redirect_to register_path and return
end
end
end
end
Additionally, i added 'and return' into the update block in my users controller after both success and failure redirects/renders