Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 根据出生日期搜索用户年龄_Ruby On Rails - Fatal编程技术网

Ruby on rails 根据出生日期搜索用户年龄

Ruby on rails 根据出生日期搜索用户年龄,ruby-on-rails,Ruby On Rails,我是Ruby on Rails的新手,我关注以下帖子: 我的应用程序有许多用户,每个用户都有一个配置文件。对于数据库中的每个配置文件,我保存他们的名字、姓氏、国家和出生日期(类型:date)。我希望我的用户能够根据他们的年龄范围(如25-35岁或35-45岁)搜索和查找其他用户。我对名字、姓氏和国家的搜索效果很好,但当我搜索他们的年龄时,搜索会返回所有的个人资料,它不会执行任何搜索 我花了几天的时间在这个问题上,不知道是什么问题。这是我的密码: 视图:searches/new.html.haml

我是Ruby on Rails的新手,我关注以下帖子: 我的应用程序有许多用户,每个用户都有一个配置文件。对于数据库中的每个配置文件,我保存他们的名字、姓氏、国家和出生日期(类型:date)。我希望我的用户能够根据他们的年龄范围(如25-35岁或35-45岁)搜索和查找其他用户。我对名字、姓氏和国家的搜索效果很好,但当我搜索他们的年龄时,搜索会返回所有的个人资料,它不会执行任何搜索

我花了几天的时间在这个问题上,不知道是什么问题。这是我的密码:

视图:searches/new.html.haml

  %h1 Custome Search

  =form_for @search do |form|
    =form.label :first_name
    %br/
    = form.text_field :first_name 
    %br/
    
    =form.label :last_name
    %br/
    = form.text_field :last_name 
    %br/
        
    =form.label :country
    %br/
    =form.select :country, options_for_select([['Norway',0], ['Sweden',1],['USA',3]]),{include_blank: true}
    %br/
    %br/
    from
    =select @search, :min_age, (18..75).to_a 
    to 
    =select @search, :max_age, (18..75).to_a

    -#from
    -#=form.select :min_age, (18..75).to_a ,{include_blank: true}
    -#to 
    -#=form.select :max_age, (18..75).to_a,{include_blank: true}  

    = form.submit "search"
= form_with(model: profile) do |form|
  - if profile.errors.any?
    #error_explanation
      %h2
        = pluralize(profile.errors.count, "error")
        prohibited this profile from being saved:
      %ul
        - profile.errors.each do |error|
          %li= error.full_message
  .field
    = form.label :first_name
    = form.text_field :first_name
  .field
    = form.label :last_name
    = form.text_field :last_name
  .field
    = form.label :date_of_birth
    = form.date_select :date_of_birth,{start_year: 1940, end_year: Time.now.year - 18}
  .field
    = form.label :country
    = form.select :country, Profile.countries.keys.to_a,{include_blank: "Selct your country"}
  .field
    = form.label :image
    = form.file_field :image
  .actions
    = form.submit
型号:search.rb

class Search < ApplicationRecord
  attr_accessor :min_age, :max_age

  def search_profiles
    profiles= Profile.all
    profiles=profiles.where(["first_name LIKE ?","%#{first_name}%"]) if first_name.present?
    profiles=profiles.where(["last_name LIKE ?","%#{last_name}%"]) if last_name.present?    
    profiles=profiles.where(["country LIKE ?","#{country}"]) if country.present?
    if min_age.present? && max_age.present?
      min = [ min_age, max_age ].min
      max = [ min_age, max_age ].max
      min_date = Date.today - min.years
      max_date = Date.today - max.years  
      profiles = profiles.where("date_of_birth BETWEEN ? AND ?", max_date, min_date)
    end  

    return profiles
    
  end
  
end
Profiles.controller:

class SearchesController < ApplicationController
  attr_accessor :min_age, :max_age

  def new
    @search = Search.new
  end
  def create
    @search=Search.create(search_params)
    redirect_to @search
  end

  def show
    @search = Search.find(params[:id])
  end
  private
  def search_params
    params.require(:search).permit(:first_name, :last_name, :country, :date_of_birth,:min_age, :max_age)
  end

end
like so
class ProfilesController < ApplicationController
  before_action :set_profile, only: [ :show, :edit, :update, :destroy ]
  before_action :authenticate_user!, except:[:index, :show, :search]
   

  def index
    @profiles = Profile.all
  end

  def show
  
  end

  # GET /profiles/new
  def new
    @profile = current_user.build_profile
  end

  # GET /profiles/1/edit
  def edit
  end

  def create
    
      @profile = current_user.build_profile(profile_params)

      respond_to do |format|
        if @profile.save
          format.html { redirect_to @profile, notice: "Profile was successfully created." }
          format.json { render :show, status: :created, location: @profile }
        else
          format.html { render :new, status: :unprocessable_entity }
          format.json { render json: @profile.errors, status: :unprocessable_entity }
        end
      end
    
  end

  def update
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile, notice: "Profile was successfully updated." }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /profiles/1 or /profiles/1.json
  def destroy
    @profile.destroy
      redirect_to profiles_url, notice: "Profile was successfully destroyed." 
  end

  private
    def set_profile
      @profile = Profile.find(params[:id])
    end

    def profile_params
      params.require(:profile).permit(:first_name, :last_name,:country, :image, :date_of_birth,:min_age, :max_age)
    end
      
end
class ProfilesController
您是否对此进行了一些基本调试,比如在搜索方法中放入
byebug
,看看
min\u age
是否真的有价值?对不起,我是初学者,我不熟悉byebug。我不是在我新的.html.haml文件的下拉菜单中给min_age传递了一个值吗?你是否缺少
select
前面的
form
?首先我尝试了form.select,但没有得到任何结果,正如你所看到的,我已经对包含form.select.On的部分代码进行了注释。在Postgres上,你可以使用
。where(“extract('year',age”)(出生日期)介于?和?“最小年龄,最大年龄)
class ProfilesController < ApplicationController
  before_action :set_profile, only: [ :show, :edit, :update, :destroy ]
  before_action :authenticate_user!, except:[:index, :show, :search]
   

  def index
    @profiles = Profile.all
  end

  def show
  
  end

  # GET /profiles/new
  def new
    @profile = current_user.build_profile
  end

  # GET /profiles/1/edit
  def edit
  end

  def create
    
      @profile = current_user.build_profile(profile_params)

      respond_to do |format|
        if @profile.save
          format.html { redirect_to @profile, notice: "Profile was successfully created." }
          format.json { render :show, status: :created, location: @profile }
        else
          format.html { render :new, status: :unprocessable_entity }
          format.json { render json: @profile.errors, status: :unprocessable_entity }
        end
      end
    
  end

  def update
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile, notice: "Profile was successfully updated." }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /profiles/1 or /profiles/1.json
  def destroy
    @profile.destroy
      redirect_to profiles_url, notice: "Profile was successfully destroyed." 
  end

  private
    def set_profile
      @profile = Profile.find(params[:id])
    end

    def profile_params
      params.require(:profile).permit(:first_name, :last_name,:country, :image, :date_of_birth,:min_age, :max_age)
    end
      
end