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?
".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?
'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.
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.