Access controller's object attributes in view page

Hi,

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

In view page of index_admin

<%=“Hi”%><%=params[:current_admin.name]%>

Hi,

See my comments inline:

Regards,
Seeni Rafiyullah Khan A,
[email protected]In Every moment, thank God.

On Thu, Sep 20, 2012 at 4:15 PM, ruby rails [email protected]
wrote:

You are trying to get the admin name from the symbol :frowning: and hence you
are
getting the issue.

In the controller, assign the resultant admin record to the instance
variable as below:

@admin=Admin.find_by_email(params[:session][:email].downcase)

In your view page, use as below:

*<%=“Hi”%><%= @admin.name%> *
*
*

Rafi A wrote in post #1076789:

Hi,

See my comments inline:

Regards,
Seeni Rafiyullah Khan A,
[email protected]In Every moment, thank God.

On Thu, Sep 20, 2012 at 4:15 PM, ruby rails [email protected]
wrote:

You are trying to get the admin name from the symbol :frowning: and hence you
are
getting the issue.

In the controller, assign the resultant admin record to the instance
variable as below:

@admin=Admin.find_by_email(params[:session][:email].downcase)

In your view page, use as below:

*<%=“Hi”%><%= @admin.name%> *
*
*
HI,

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

In my view page

*<%=“Hi”%><%= @admin.name%>

Regards,
Seeni Rafiyullah Khan A,

On Thu, Sep 20, 2012 at 4:36 PM, ruby rails [email protected]
wrote:

getting the issue.
*
else
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.

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 :slight_smile:

<% 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


Hassan S. ------------------------ [email protected]

twitter: @hassan