Question

I have this idea that I want "Logged in" to be show in the upper corner of my webpage when user_signed_in? is true, but when user_signed_in? is false then I want it to only simply put "Login".

user_signed_in? is the helper method for Devise. I think.

How do I actually make this work. I tried this:

<%= if user_signed_in? => true puts "Logged In" else puts "Login" %>
<% end %>
<% end %>

This doesn't work though. Any ideas ?

Was it helpful?

Solution

you should look into Ruby syntax a little more (here, for example). Try this:

<% if user_signed_in? %>
  Logged In
<% else %>
  <%= link_to 'Login', ... %> # substitute the ... for whatever path goes to login
<% end %>

Note that the <%= part is only used when the embedded ruby will actually be returning content that will be rendered by the browser (aka converted to html). Since all your embedded ruby in your example is only control flow, don't use the =, since the browser won't actually see any of it. However, the link_to method returns a link, which is actual rendered content, so we need the =.

Also note that since this is a view file--and thus is basically an enhanced html file--you don't need the "puts" for raw content like "Logged in". In fact, puts wouldn't bring the content to the browser, but instead to wherever STDOUT leads in Rails apps (the logger? not sure)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top