2013-04-24 73 views
0

我有一個簡單的約會應用程序設計,我的用戶模型有男性和女性的兩個布爾字段,我想當用戶註冊爲男性,女性用戶的情況應顯示給他作爲朋友加入,如果它也是女性,則應顯示男性用戶。我的用戶模型如下如何取女性用戶郵寄紅寶石在軌道上

t.string :email,    :null => false, :default => "" 
    t.string :bio,    :null => false, :default => "update whatsup with yourself and click here to edit it" 
    t.string :username,    :null => false, :default => "" 
    t.string :encrypted_password, :null => false, :default => "" 
    t.boolean :admin 
    t.boolean :author 
    t.boolean :male 
    t.boolean :female 

,我什麼地方實施的友誼模式

回答

0

在你的控制器動作,加上該位。

@opposite_sex_users = nil 
if current_user.male && !current_user.female 
    @opposite_sex_users = User.where(:male => false, :femail => true) 
elsif !current_user.male && current_user.female 
    @opposite_sex_users = User.where(:male => true, :femail => false) 
end 

你真的沒有給我足夠的信息來不幸的是解決你的問題。假設你想在頁面控制器中使用home動作,你可以這樣做。

class PagesController < ApplicationController 
    def home 
    @opposite_sex_users = nil 
    number_of_users_to_show = 5 # change this to the number of users you want to show 
    if current_user.male && !current_user.female 
     @opposite_sex_users = User.where(:male => false, :femail => true).sample(number_of_users_to_show) 
    elsif !current_user.male && current_user.female 
     @opposite_sex_users = User.where(:male => true, :femail => false).sample(number_of_users_to_show) 
    end 
    end 
end 

而在你的路線,

root :to => "pages#home" 

而在你的意見/頁/ home.html.erb

<%- @opposite_sex_users.each do |user| %> 
    <!-- displays user email %> 
    <%= user.username %> 
<% end %> 
+0

我如何使用這在我的意見嗎? – daniel 2013-04-25 05:02:42

+0

您想使用哪種控制器和操作? – 2013-04-25 05:05:01

+0

我想要一個情況,如果登錄的男性用戶,如果他訪問索引頁面顯示所有用戶,他將顯示女性用戶,如果女性用戶,相反發生感謝 – daniel 2013-04-25 05:07:24