简体   繁体   English

如何根据 Rails 中的某些条件显示男性或女性?

[英]How do I display male or female based on certain conditions in Rails?

I am new to rails and currently working on a dating application as a portfolio project.我是 Rails 新手,目前正在开发一个约会应用程序作为投资组合项目。 However, on my home page, I want to be able to display/render Male or Female data based on the gender of the current_user .但是,在我的主页上,我希望能够根据current_user的性别显示/呈现男性或女性数据。 That is if the logged-in user is a male, he will only be able to view people of the opposite gender since it's a dating application.也就是说,如果登录用户是男性,他将只能查看异性,因为它是一个约会应用程序。

I am using Devise gem and included a custom field called gender .我正在使用 Devise gem 并包含一个名为gender的自定义字段。 Please, someone should help me with how to do this.拜托,有人应该帮助我如何做到这一点。

class UsersController < ApplicationController
  def index
    @user = current_user
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end


User Table用户表

create_table "users", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.text "short_bio"
    t.string "gender"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "unconfirmed_email"
    t.integer "like"
    t.string "image_url"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

User Model用户 Model

class User < ApplicationRecord
  has_one_attached :image
  has_many_attached :pictures

  has_many :messages
  has_many :likes
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :validatable, :trackable

  # validates :short_bio, presence: true, length: {maximum: 500}
  validates :email, presence: true, uniqueness: { case_sensitive: false }, length: {minimum: 5, maximum: 50}
  

end

You can only load users with the opposite gender than the current user like this:您只能像这样加载与当前用户不同性别的用户:

@users = User.where.not(gender: current_user.gender)

Probably you need in the model two columns.可能你需要在 model 两列。 It is just example as idea这只是想法的例子

You can use gender (it's not binary) instead of sex您可以使用性别(它不是二进制)而不是性别

You can use other data type of course您当然可以使用其他数据类型

t.integer :sex
t.integer :look_for_sex

And for example:例如:

0 -- female, 1 -- male 0 -- 女性,1 -- 男性

And then接着

User.where(sex: current_user.look_for_sex, look_for_sex: current_user.sex)

will return only users that current user want to date将仅返回当前用户想要约会的用户

Thank you for all that responded.谢谢大家的回复。 I didn't have to refactor my code but just added the code below and that fixed my problem:我不必重构我的代码,只需添加下面的代码即可解决我的问题:

@users = current_user == 'Male' ? User.where(gender: 'Male') : User.where(gender: 'Female')

In this case, if the current user is a 'Male', he will only have access to 'Female' and vice versa.在这种情况下,如果当前用户是“男性”,他将只能访问“女性”,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM