2011-07-23 26 views
2

登錄這是我的會話的控制器代碼隱藏一個div,而用戶在軌

def create 
    user = User.authenticate(params[:login], params[:password]) 
    if user 
     session[:user_id] = user.id 
     redirect_to_target_or_default root_url, :notice => "Logged in successfully." 
    else 
     flash.now[:alert] = "Invalid login or password." 
     render :action => 'new' 
    end 
    end 

我需要位於佈局/ application.html.erb顯示一個div id="welcomebuttons"當用戶不在會議(註銷),但完全消失並在用戶登錄時保持隱藏狀態。我嘗試將javascript:hideDiv_welcomebuttons()添加到if user,但當然不起作用。

任何人都可以幫忙嗎?

回答

1
在應用程序佈局

<% if session[:user_id].nil? %> 
    <div id="welcomebuttons"> 
    </div> 
<% end %> 
0

我使用塊助手這樣的(只是將它們添加到您的application_helper.rb,你是好去):

# application_helper.rb 
def not_logged_in(&block) 
    capture(&block) unless session[:user_id] 
end 

def logged_in(&block) 
    capture(&block) if session[:user_id] 
end 

#application.html.erb 
<div>I'm visible for everyone</div> 

<%= logged_in do %> 
    <div>I'm only visible if you are logged in</div> 
<% end %> 

<%= not_logged_in do %> 
    <div>I'm only visible unless you are logged in</div> 
<% end %>