【问题标题】:how to fetch female users to mail in ruby on rails如何在 ruby​​ on rails 中获取女性用户发送邮件
【发布时间】:2013-04-18 15:58:43
【问题描述】:

我正在设计一个简单的约会应用程序,我的用户模型有男性和女性两个布尔字段,我想要一个用户注册为男性的情况,应该向他显示女性用户添加为朋友,如果它也是女性,应该显示男性用户。我的用户模型如下

  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

我已经实现了友谊模型

【问题讨论】:

    标签: ruby-on-rails boolean


    【解决方案1】:

    在你的控制器动作中,添加这个位。

     @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"
    

    在你的意见/pages/home.html.erb

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

    【讨论】:

    • 请问我该如何在我的观点中使用它?
    • 你想在哪个控制器和动作上使用它?
    • 我想要一个情况,如果一个登录的男性用户,如果他访问显示所有用户的索引页面,他将被显示为女性用户,如果是女性用户,则相反,谢谢
    • 查看编辑后的回复。抱歉,我不得不对您的应用做出一些假设。但它真的足够彻底,你应该能够弄清楚。
    • 非常感谢垃圾收集我发现你的回答对我有很大帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2017-04-30
    • 2016-12-18
    • 1970-01-01
    • 2011-01-24
    • 2010-10-15
    相关资源
    最近更新 更多