I am new to ROR and learning it. In my controller I have an admins
record and I am passing that admin object to the admin’s view page to
get the name of the admin. But when I try to access the name it is
showing error as “undefined method `name’ for :current_admin:Symbol”…
Please help…
Please find my code below
Sessions Controller
def create
admin=Admin.find_by_email(params[:session][:email].downcase)
if admin && admin.authenticate(params[:session][:password])
redirect_to(admins_index_path(:current_admin=>admin))
end
end
I tried with the the below code. But still getting the same result in
view page as “undefined method `name’ for nil:NilClass”
def create
@admin=Admin.find_by_email(params[:session][:email].downcase)
user=User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
redirect_to(dashboard_path(:user=>user))
else
redirect_to(admins_index_path(:current_admin=>@admin))
end
end
This is different issue. Check whether there is an admin in your db with
the email address which you are passing through params[:session][:email]
? And for the backward compatibility, use the below logic in your view
so
that you can get the clarity in what you are doing
<% if !@admin.blank? %>
<%=“Hi”%><%= @admin.name%>
<%else%>
no records
<%end%>
On Thu, Sep 20, 2012 at 3:45 AM, ruby rails [email protected]
wrote:
I am new to ROR and learning it. In my controller I have an admins
record and I am passing that admin object to the admin’s view page to
get the name of the admin. But when I try to access the name it is
showing error as “undefined method `name’ for :current_admin:Symbol”…
Sessions Controller
def create
admin=Admin.find_by_email(params[:session][:email].downcase)
if admin && admin.authenticate(params[:session][:password])
redirect_to(admins_index_path(:current_admin=>admin))
Side note –
you probably want to pass that as (:current_admin_id=>admin.id)
You’re doing a redirect, which means the request will be sent to the
controller and method associated with “admins_index_path”.
In that controller and method, you must set the variables you need,
e.g. @admin = Admin.find(params[:current_admin_id]
Then your view can use attributes e.g. @admin.name