2010-01-21 9 views
7

मेरे पास एक ऐसा एप्लिकेशन है जहां लेआउट में मेरे पास user_name div है जो विभिन्न लॉग इन करता है कि वे लॉग इन हैं या नहीं, व्यवस्थापक हैं, आदि। अभी मेरा कोड निम्न है :रेल एल्स पर रूबी यदि प्रश्न

<% if current_user.role == "admin" %> 
    <p id="admintxt">You are an admin!</p> 
     <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
    <% elsif current_user %> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
    <% else %> 
<%= link_to "Register", new_user_path %> 
<%= link_to "Login", login_path %> 
<% end %> 

मैं एक current_user सहायक पहले से ही है और सब कुछ ठीक काम कर रहा था जब कोड सिर्फ था: जब मैं यह एक elsif बयान, जब मैं एक व्यवस्थापक के रूप में लॉग इन कर रहा हूँ

<% if current_user %> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
<% else %> 
    <%= link_to "Register", new_user_path %> 
    <%= link_to "Login", login_path %> 
<% end %> 

अब यह काम करता है और मुझे सही लिंक के साथ प्रदर्शित पाठ मिलता है। NilClass त्रुटि ... इसके अलावा मेरे current_user सामान अपने आवेदन नियंत्रक में घोषित किया जाता है इस प्रकार है:: जब मैं एक व्यवस्थापक उपयोगकर्ता नहीं कर रहा हूँ/लॉग आउट, मैं एक अपरिभाषित विधि `शून्य के लिए भूमिका 'प्राप्त

helper_method :current_user 


private 

def current_user_session 
    return @current_user_session if defined?(@current_user_session) 
    @current_user_session = UserSession.find 
end 

def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session && current_user_session.record 
end 

कोई भी विचार क्या मैं जो परिणाम चाहता हूं उसे प्रदर्शित करने के लिए कर सकता हूं? "अगर वे व्यवस्थापक के बराबर भूमिका विशेषता वाले उपयोगकर्ता हैं तो उन्हें कुछ टेक्स्ट और लॉग इन लिंक मिलते हैं, अगर वे केवल उपयोगकर्ता हैं तो वे लॉग इन लिंक प्राप्त करते हैं, और यदि वे लॉग इन नहीं हैं तो वे रजिस्टर और लॉगिन लिंक प्राप्त करते हैं ।

धन्यवाद!

उत्तर

11
<% if current_user %> 
    <% if current_user.role == "admin" %> 
    <p id="admintxt">You are an admin!</p> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
    <% else %> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
    <% end %> 
<% else %> 
    <%= link_to "Register", new_user_path %> 
    <%= link_to "Login", login_path %> 
<% end %> 

या रेल के साथ> = 2,3

<% if current_user.try(:role) == "admin" %> 
    <p id="admintxt">You are an admin!</p> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
<% elsif current_user %> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
<% else %> 
    <%= link_to "Register", new_user_path %> 
    <%= link_to "Login", login_path %> 
<% end %> 
+1

इस पर ध्यान देने से मुझे वास्तव में हमाल की सराहना होती है। :) – jerhinesmith

+0

मैं 'erb' लिख रहा हूं लेकिन कभी नहीं महसूस किया कि यह तब तक कितना बदसूरत है जब तक मैंने इसे देखा नहीं। अब मैं – Hanmaslah

3
<% if current_user and current_user.role == "admin" %> 

जब कोई उपयोगकर्ता में लॉग इन यह त्रुटि को रोकने चाहिए, लेकिन आप पूरे ब्लॉक पुनर्गठन सकता है में वर्तमान_user के खिलाफ अनावश्यक परीक्षणों को हटाने के लिए आदेश।

5

वर्तमान उपयोगकर्ता लूप में भूमिका जांच छुपाएं, जिसका आपके सशर्त को सरल बनाने का दुष्प्रभाव है।

<% if current_user %> 
    <%= content_tag(:p, "You are an admin!", :id=>"admintxt") if current_user.role == "admin" %> 
    <%= link_to "Edit Profile", edit_user_path(:current) %> 
    <%= link_to "Logout", logout_path %> 
<% else %> 
    <%= link_to "Register", new_user_path %> 
    <%= link_to "Login", login_path %> 
<% end %> 
+0

पर प्रतिक्रिया की सराहना करता हूं, क्योंकि मैं लूप लिख रहा था, मुझे पता था कि इसे सरल बनाने का एक तरीका निश्चित रूप से था। मैं बहुत सी सीख रहा हूँ! –

संबंधित मुद्दे