2013-12-14 55 views
1

所以我創建了一個具有用戶個人資料的網絡應用程序,用戶可以根據興趣等搜索其他用戶。但有時用戶不會填寫他們的個人資料的某些領域,即關於我可能留空白。所以我怎麼能添加的能力,用戶只能看到他們填寫的配置文件的內容。換句話說,他們無法看到尚未完成的其他用戶配置文件的部分。只能看到已填寫的個人資料的內容

user_profile.html.erb

<%= render "shared/header" %> 

<div id="landing_welcome_page"> 
    <div class="container"> 
    <div class="row"> 

     <%#= Profile image upload %> 
     <div class="span4"> 
     <%= user_avatar(current_user) %> 
     <%#= Space w line %> 
     <div class="name"></div><br> 

     <%#= Please bare in mind these are strickly temporary placeholders i.e whitespace %> 
     <%= render 'social' %> 
     </div> 

     <div class="span8"> 
     <%# User name %> 

      <span class="name1"> 
      <% if current_user.first_name.blank? %> 
      <%= current_user.first_name.present? ? current_user.first_name : link_to('Finish your profile', edit_account_path)%> 
      <% else %> 
      <%= current_user.first_name %> <%= current_user.last_name %> 
      <% end %> 
      </span> 
      <span class="side-buttons"> 
     <div class="name"></div> 
      </span> 
     </span> 
     </div> 

     <div class="row"> 
     <br /> 
     <div class="span6"> 
      <%# User occupation %> 
      <i class="fa fa-usd"></i>: 
      <%= best_in_place current_user, :occupation, nil: 'Add occupation' %> 
     </div> 

     <div class="addy"> 
      <div class="span2"> 
      <%# User address %> 
      <i class="fa fa-home"></i>: 
      <%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %> 
     </div> 
     </div> 

     <div class="span6"> 
     <%# User gender %> 
    <br /> 
     <% if current_user.gender == "M" || "male" %> 
     <i class="fa fa-male"></i> Male 
     <% else %> 
     <i class="fa fa-female"></i> Female 
     <% end %> 
     </div> 

     <div class="span2"> 
     <!-- Code to calculate age by user birthday --> 
    <br /> 
     Age: <%= user_birthday %> 
     </div> 

     <div class="span8"></div> 

     <div class="span8"><div class="name"></div></div> 
     <div class="span8"> 
     <div class="tabbable"> <!-- Only required for left/right tabs --> 
      <ul class="nav nav-tabs"> 
      <li class="active"><a href="#tab1" data-toggle="tab">About me</a></li> 
      <li><a href="#tab2" data-toggle="tab">Photos</a></li> 
      <li><a href="#tab3" data-toggle="tab">Personality</a></li> 
      </ul> 
      <div class="tab-content"> 
      <div class="tab-pane in active" id="tab1"> 
       <% @questions_for_about.each_with_index do |question, index| %> 
       <div class="question"> 
       <h4 class="user_questions"> 
        <%= index + 1 %>. <%= question.question %> 
        <%= link_to ("<i class='icon-edit'></i>".html_safe), 
        edit_user_question_path(current_user, question), 
        remote: true, class: "edit_link_#{question.id}" %> 
       </h4> 
       <div class="answer" id="answer_<%= question.id %>"> 
        <%= answer_for(question) %> 
       </div> 
       </div> 
       <% end %> 
      </div> 
      <div class="tab-pane" id="tab2"> 
       <div class="page-header"> 
       <%= form_for Photo.new do |f| %> 
       <span class="btn btn-success fileinput-button"> 
        <i class="icon-plus icon-white"></i> 
        <span>Add photos...</span> 
        <%= f.file_field :file, multiple: true, name: "photo[file]" %> 
       </span> 
       <% end %> 
       <div class="clearfix"></div> 
       </div> 
       <div class="photos_cont"> 
       <div class="col-sm-6 col-md-3"> 
        <span class="gallery"><%= render current_user.photos %></span> 
       </div> 
       </div> 
      </div> 
      <div class="tab-pane" id="tab3"> 
       <% @questions_for_personality.each_with_index do |question, index| %> 
       <div class="question"> 
       <h4 class="user_questions"> 
        <%= index + 1 %>. <%= question.question %> 
        <%= link_to ("<i class='icon-edit'></i>".html_safe), 
        edit_user_question_path(current_user, question), 
        remote: true, class: "edit_link_#{question.id}" %> 
       </h4> 
       <div class="answer" id="answer_<%= question.id %>"> 
        <%= answer_for(question) %> 
       </div> 
       </div> 
       <% end %> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    <%= render '/shared/footer' %> 
    </div> 
</div> 

<script id="template-upload" type="text/x-tmpl"> 
<div class="upload"> 
{%=o.name%} 
<div class="progress"><div class="bar" style="width: 0%;"></div></div> 
</div> 
</script> 
<script> 
var fb_param = {}; 
fb_param.pixel_id = '6009056882201'; 
fb_param.value = '0.00'; 
(function(){ 
    var fpw = document.createElement('script'); 
    fpw.async = true; 
    fpw.src = '//connect.facebook.net/en_US/fp.js'; 
    var ref = document.getElementsByTagName('script')[0]; 
    ref.parentNode.insertBefore(fpw, ref); 
})(); 
</script> 

<noscript> 
    <img height="1" src="https://www.facebook.com/offsite_event.php?id=6009056882201&amp;value=0" style="display:none;" width="1"/> 
</noscript> 
<script type="text/javascript"> 
// remove default datepicker event 
jQuery(document).off('best_in_place:datepicker'); 

jQuery(document).on('best_in_place:datepicker', function(event, bip, element) { 
    // Display the jQuery UI datepicker popup 
    jQuery(element).find('input') 
    .datepicker({ 
    format: element.data('date-format') 
    }) 
    .on('hide', function(){ 
    bip.update(); 
    }) 
    .on('changeDate', function(){ 
    $(this).datepicker('hide'); 
    }) 
    .datepicker('show'); 
}); 
</script> 
+0

這個問題被問了近一個月前。你有沒有機會查看任何提供的答案? – zeantsoi

回答

1

,你有可以擴展到覆蓋包含信息塊整體div的條件句。

例如:

<div class="addy"> 
     <div class="span2"> 
     <%# User address %> 
     <i class="fa fa-home"></i>: 
     <%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %> 
    </div> 
</div> 

可能是

<div class="addy"> 
    <% if current_user.address.present? %> 
     <div class="span2"> 
     <%# User address %> 
     <i class="fa fa-home"></i>: 
     <%= current_user.address %> 
     </div> 
    <% else %> 
    <%= link_to('Add Address', edit_account_path) %> 
    <% end %> 
</div> 

然後,它只會顯示的鏈接,如果地址不存在。

0

看起來像你在正確的軌道上。基本上,你要顯示/隱藏User模型中的每個用戶的屬性,你需要添加一個條件,評估對current_user屬性是否爲present?

<span class="name1"> 
    <% if current_user.first_name.blank? %> 
     <%= link_to('Finish your profile', edit_account_path) %> 
    <% else %> 
     <%= current_user.first_name %> <%= current_user.last_name %> 
    <% end %> 
</span> 

或者,您可以繼續使用三元運營商的單線評估,因爲你已經在某些情況下做:

<%= current_user.address.present? ? current_user.address : link_to('Add Address', edit_account_path) %> 
相關問題