Hi all, I''m trying to do a condition in my application.html.erb where if a user is based off of a STI it has a "special" section in the navigation. The following code correctly prints out the user type (either User or Client) [code] <%= current_user.class if logged_in? %> [/code] However, when I try to do an if search (example below) it work at all. [code] <%= content_tag(:li, link_to ("Products", productss_path)) if current_user.class == "Client" %> [/code] If I change "Client" in the above code to "User" it still doesn''t work. Is there some special case or usage required to handle current_user.class in an if loop? Thanks in advance! -Tony -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tony Tony wrote:> <%= content_tag(:li, link_to ("Products", productss_path)) if > current_user.class == "Client" %>Oops - typo in the above code: products_path not productss_path Any help is appreciated! -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 March 2010 15:21, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> [code] > > <%= content_tag(:li, link_to ("Products", productss_path)) if > current_user.class == "Client" %> > > [/code] > > If I change "Client" in the above code to "User" it still doesn''t work. > Is there some special case or usage required to handle > current_user.class in an if loop?".class" doesn''t return a string, it returns a class... <%= content_tag(:li, link_to ("Products", productss_path)) if current_user.class == Client %> but you might be better using "is_a?(base_class)" - depends what the intention of your code is: <%= content_tag(:li, link_to ("Products", productss_path)) if current_user.is_a?(Client) %> -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote:> ".class" doesn''t return a string, it returns a class... > > <%= content_tag(:li, link_to ("Products", productss_path)) if > current_user.class == Client %> > > but you might be better using "is_a?(base_class)" - depends what the > intention of your code is: > > <%= content_tag(:li, link_to ("Products", productss_path)) if > current_user.is_a?(Client) %>THANK YOU MICHAEL! I get the hard stuff working but simple stuff like this gets me stuck often. Besides the craziness of the Rails API (although it IS helpful), if there a place where I can learn this kind of thing simple thing? Thanks again! -Tony -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 March 2010 15:36, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> is there a place where I can learn this kind of thing simple thing?''Fraid you''ve just gotta read the docs and remember as much as you can - and be suspicious! If something doesn''t "work" then make sure all your assumptions are correct - we normally assume things like return values; object types; variables not being nil; existence of methods... all that stuff :-) I can''t recommend enough this search in Google: "ruby rails api <your problem>" I had an inkling about the solution to your problem and Googled for "ruby api object", that took me to the docs for the .class method, and the answer was right there. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote:> On 31 March 2010 15:36, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> is there a place where I can learn this kind of thing simple thing? > > ''Fraid you''ve just gotta read the docs and remember as much as you can > - and be suspicious! If something doesn''t "work" then make sure all > your assumptions are correct - we normally assume things like return > values; object types; variables not being nil; existence of methods... > all that stuff :-) > > I can''t recommend enough this search in Google: > > "ruby rails api <your problem>" > > I had an inkling about the solution to your problem and Googled for > "ruby api object", that took me to the docs for the .class method, and > the answer was right there.Thank you! I was able to find the .class declaration via your search term. I guess I just didn''t know that the "current_user" part was an object. I''m telling you - simple stuff. Luckily I''ve gotten far from using basic knowledge and hacking/stitching things together until they work. I hate it though - I''d love to know my programming was solid. I''m going to dedicate a few weeks to learning the basics to avoid more embarrassing situations like these. :-) -Tony -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Mar-31 15:41 UTC
Re: Re: Re: Single Table Inheritance and .class problem
On 31 March 2010 16:38, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I was able to find the .class declaration via your search term. I guess > I just didn''t know that the "current_user" part was an object. I''m > telling you - simple stuff.In Ruby, *everything* is an object :-) -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 March 2010 16:38, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Michael Pavling wrote: >> On 31 March 2010 15:36, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> is there a place where I can learn this kind of thing simple thing? >> >> ''Fraid you''ve just gotta read the docs and remember as much as you can >> - and be suspicious! If something doesn''t "work" then make sure all >> your assumptions are correct - we normally assume things like return >> values; object types; variables not being nil; existence of methods... >> all that stuff :-) >> >> I can''t recommend enough this search in Google: >> >> "ruby rails api <your problem>" >> >> I had an inkling about the solution to your problem and Googled for >> "ruby api object", that took me to the docs for the .class method, and >> the answer was right there. > > Thank you! > > I was able to find the .class declaration via your search term. I guess > I just didn''t know that the "current_user" part was an object. I''m > telling you - simple stuff.In ruby all objects are Objects. Colin -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Not sure if the best part was identical answers or identical answer times! This forum kicks butt for putting up and helping us newbies out. Thank you guys. -Tony -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 March 2010 16:41, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 31 March 2010 16:38, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> I was able to find the .class declaration via your search term. I guess >> I just didn''t know that the "current_user" part was an object. I''m >> telling you - simple stuff. > > In Ruby, *everything* is an object :-)Michael, we will have to stop meeting like this, people will begin to talk. Actually I baulked at saying everything is an object, in some languages even the operators are objects and I think that is not the case in ruby. Colin -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.