2013-12-13 68 views
4

所以我創建了一個具有用戶配置文件的Web應用程序,用戶可以根據興趣等搜索其他用戶。我如何添加一個功能,以便用戶可以收藏個人配置文件?即用戶A找到用戶B並喜歡他們看到的內容,並且可以單擊「收藏該個人資料」按鈕,也許它已加星標並存儲?代碼是什麼樣子的?只是一個想法,但我願意接受所有想法。favouriting users profiles in rails

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> 

回答

1

創建與用戶一旦創建了最喜愛的模型

一個has_and_belongs_to_many關係最喜愛的模型,並建立了關係,你可以做這樣的事情:

@user.favorites = [ user1, user2, user3 ] 
@user.favorites << user4 

給用戶指定收藏夾,或者用類似的方式顯示它們:

<%= @user.favorites.map(&:name).to_sentence %> 

你會發現你需要知道如何做到這一點這裏的一切:http://guides.rubyonrails.org/association_basics.html

+0

非常感謝@Donovan,關心擴大你的答案有點至於如何將功能一旦工作模型創建? –

+1

當然,但我建議你在導軌指南中閱讀它們,如果你不熟悉它的話。 – Donovan