Ruby on rails pathname considered uninitialized constant in controller - ruby-on-rails

In my Ruby on Rails app, when a user's authentication succeeds, I want to send him/her to a specific home page based on the account they belong to.
I tried adding an 'if' clause in my Sessions controller as follows :
def create
if user = User.authenticate(params[:login], params[:password])
session[:user_id] = user.id
if user.account.name == "X"
redirect_to Xhome_path, :notice => "Logged in successfully"
else
redirect_to home_path, :notice => "Logged in successfully"
end
else
flash.now[:alert] = "Invalid login/password combination"
render :action => 'new'
end
end
Copying the already existing following line in routes.rb :
match '/home' => "users#home", :as => "home"
I added this one :
match '/Xhome' => "users#Xhome", :as => "Xhome"
This does not work and gives the following results :
if a user with user.account.name 'X' logs in, the browser displays the following error message :
NameError in SessionsController#create
uninitialized constant SessionsController::Xhome_path
however, typing the mydomain.com/Xhome URL once this user is authenticated works well and brings the expected Xhome view, which seems to prove that there is no problem on the routes side.
Do you know where this NameError issue can come from ? Why can't I use Xhome_path just as I did with home_path ?

Do a rake routes and check for the available route names and their corresponding path helpers. Check if the path name is actually Xhome_path. If no path name exist, then do
redirect_to '/Xhome', :notice => "Logged in successfully"

Related

No route match even when route is defined

I have just started working in rails.
I have a controller that redirects to another controller to send an email for emailid verification.
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
redirect_to :controller => 'verify', :action => 'sendmail'
else
format.html { render :new }
end
and this is the route in routes file
resources :verify
but still the program is returning an error
No route matches {:action=>"sendmail", :controller=>"verify"}
This is the code of the controller to which I am redirecting
def sendmail
# type = 1 for email and 2 for reseting password
randomNumber=rand()
verification[:value]=BCrypt::Password.create(randomNumber, :cost => 1)
verification[:type] = 1
if verification.save
Usermail.registered(#verification).deliver
end
I think Verify (singular) as a controller name won't easily be mapped by resources, instead just add a custom match
get '/verify/sendmail', to: 'verify#sendmail', as: :verification_mail
Then do
redirect_to verification_mail_path

Rails is redirectin to a site i didnt choose

I followed the tutorial from railscast about authentication. Because I have two models that should be authenticated I changed the session create code a little bit:
def create
if User.find_by_username(params[:username]) != nil
user = User.find_by_username(params[:username])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path, notice: "Eingeloged als User"
end
elsif Admin.find_by_username(params[:username]) != nil
admin = Admin.find_by_username(params[:username])
if admin && admin.authenticate(params[:password])
session[:user_id] = admin.id
redirect_to adminpage_index_path, notice: "Eingeloged als Admin"
end
else
flash[:error] = "Benutzername oder Password ist falsch"
render 'new'
end
end
If I login as user it works, and when I type in a false password it also works. But somehow when I want to login as admin I get the error:
Template is missing
Missing template sessions/create
I don't know why I get this error! I mean where does my code say that it should redirect to session create ? Thanks
My routes:
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
resources :users
resources :sessions
Look at the logic in your controller action:
if admin && admin.authenticate(params[:password])
session[:user_id] = admin.id
redirect_to adminpage_index_path, notice: "Eingeloged als Admin"
end
If you have provided a valid username and an invalid password, what is going to happen?
You're not going to get into that if statement - you're going to continue through the controller action, fall out the bottom, and the default behaviour in Rails is to render a view at the end of an action if no other behaviour is requested. Hence it's trying to render the create view for your create action - which doesn't exist.
You need to handle the invalid-password case for both users and admins.
Also, why are you calling find_by_username twice for both users and admins?
I think you are missing the else part in Admin authentication..
It should be,
def create
if User.find_by_username(params[:username]) != nil
user = User.find_by_username(params[:username])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path, notice: "Eingeloged als User"
else
redirect_to root_path, notice: "Your Notice"
end
elsif Admin.find_by_username(params[:username]) != nil
admin = Admin.find_by_username(params[:username])
if admin && admin.authenticate(params[:password])
session[:user_id] = admin.id
redirect_to adminpage_index_path, notice: "Eingeloged als Admin"
else
redirect_to root_path, notice: "Your Notice"
end
else
flash[:error] = "Benutzername oder Password ist falsch"
render 'new'
end
end
So it looks like the faulty line is the redirect_to adminpage_index_path, notice: "Eingeloged als Admin" . It may help if you post your routes (run rake routes from your terminal). If it's saying the template is missing though, likely you don't have an index.html.erb in your app/views/adminpages/ folder? Is there a file there? (Also make sure you have the appropriate app/contollers/adminpages_controller and appropriate index action.

how to route to a different page in ruby on rails

I have a session controller like
def create
user = User.find_by_email(params[:session][:email])
if user && 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
if you notice on successful sign in it does redirect_to user which is the show action in the User controller.
But say instead of that I have to go to the new action of user controller then how do i do that?
redirect_to user is just a shortcut for redirect_to url_for(user), which generates the url for a given resource (url_for) and then redirects to it.
If you want to redirect to another url, you can use the path helper.
redirect_to new_user_path
You could also use the url_for helper to generate an url.
redirect_to url_for(:controller => "users", :action => "new")
Which can be shortened to
redirect_to :controller => "users", :action => :new
You can even specify an url directly (redirect_to '/users/new') - which I would not recommend, as you can't change your routing later without changing all the urls.
The docs
As you can see, there are many ways to specify an url to redirect_to in rails. Take a look at the documentation for all of them
To render an action from another controller:
render 'users/new'
See: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

How to authenticate users in Authlogic with usernames that are unique to an account only

For the life of me, I can't get this to work. I have an application where each account has a unique subdomain. Within that account, users have a unique username but the username may not be unique across the application as a whole.
def create
#user_session = #current_account.user_sessions.new(params[:user_session])
#user = #current_account.users.find_by_username_or_email(params[:user_session][:username])
if #user_session.save
flash[:notice] = "Welcome back #{current_user.first_name}"
if #user_session.user.account.is_new? && !#current_account.stop_wizard?
redirect_to :controller => "site", :action => "welcome", :id => "one"
else
redirect_to dashboard_url
end
else
render :action => :new
end
end
This seems to fail on the if #user_session.save line because it validates against the first instance of the username within the database and not against the username that is scoped to the current account.
Any advice would be appreciated.
Thanks
Robin
The with scope option when creating my session was the answer.

ruby on rails session information grab

When i run in my page this code :
<% user = User.find(session[:userid]) %>
i get the error :
line #1 raised:
Couldn't find User without an ID
although i have in my authentification in my sessions_controller this :
def create
if user = User.authenticate(params[:username],params[:password])
session[:user_id]= user.id
session[:language_id]= user.language_id
User.find(user.id).update_attributes(:last_login => Time.now)
redirect_to root_path , :notice => (I18n.t :"session.login_success")
else
flash.now[:alert] = (I18n.t :"session.error")
render :action => 'new'
end
end
and the session should contain the userid
In your log-in you set session[:user_id], and you try to find session[:userid] (note the spurious underscore). That's why.
Your controller is looking for params session[:user_id], but in your views it is session[:userid] so it will complain that session[:user_id] is nil.

Resources