views:

266

answers:

1

I have the following code in a partial view file: "_status.html.erb"

<%=
    if session[:user].nil?
        "welcome new user"
         link_to( 'Sign in', login_path) 
    else
         render ( :text => "user name:")
         h(session[:user].name)
     end
 %>

But I only see the values of:

 session[:user].name

(not the text "user name:")

Not sure what I'm missing here, any ideas?

Thanks

+3  A: 

You really don't need to call render, unless it's located in another partial.

In erb:

<% if session[:user].nil? -%>
  welcome new user
  <%= link_to( 'Sign in', login_path) %>
<% else -%>
  user name
  <%= h(session[:user].name) %>
<% end -%>

Or in Haml:

- if session[:user].nil?
  welcome new user
  = link_to( 'Sign in', login_path)
- else
  user name
  = h(session[:user].name)
jdl
that's great, thanks. I didn't notice the -%> part until now. Thanks!
cbrulak
It's the <%= versus <% that is important. The -%> simply suppresses the blank line that would otherwise be sent to the output.
jdl