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]%> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hi, See my comments inline: Regards, Seeni Rafiyullah Khan A, <srkhan-E9Vo0z8jLxLQT0dZR+AlfA@public.gmane.org>*In Every moment, thank God.* On Thu, Sep 20, 2012 at 4:15 PM, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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]%> > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >You are trying to get the admin name from the symbol :( 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%> * * * -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Rafi A wrote in post #1076789:> Hi, > > See my comments inline: > > Regards, > Seeni Rafiyullah Khan A, > <srkhan-E9Vo0z8jLxLQT0dZR+AlfA@public.gmane.org>*In Every moment, thank God.* > > > > On Thu, Sep 20, 2012 at 4:15 PM, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: > >> >> >> >> >> > You are trying to get the admin name from the symbol :( 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Regards, Seeni Rafiyullah Khan A, On Thu, Sep 20, 2012 at 4:36 PM, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Rafi A wrote in post #1076789: > > Hi, > > > > See my comments inline: > > > > Regards, > > Seeni Rafiyullah Khan A, > > <srkhan-E9Vo0z8jLxLQT0dZR+AlfA@public.gmane.org>*In Every moment, thank God.* > > > > > > > > On Thu, Sep 20, 2012 at 4:15 PM, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > > wrote: > > > >> > >> > >> > >> > >> > > You are trying to get the admin name from the symbol :( 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 > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > > This is different issue. Check whether there is an admin in your db withthe 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%> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder
2012-Sep-20 14:21 UTC
Re: access controller''s object attributes in view page
On Thu, Sep 20, 2012 at 3:45 AM, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.