Ruby on rails 如何在rails中设置字段默认值

Ruby on rails 如何在rails中设置字段默认值,ruby-on-rails,Ruby On Rails,在我的rails应用程序中,我尝试从头开始进行基于位置的搜索,我有三个字段用于位置状态、城市和区域。在州有许多城市的地方,城市属于一个州,城市有许多地区,地区属于一个城市 它的工作完全好,我现在用户可以选择州,城市和地区,并可以完成搜索,但我想要的是,如果用户登录并选择一个州,城市和地区,它应该被保存在他的帐户作为默认位置,因此,即使他注销并再次登录,他不应该再添加他的州,城市和地区 这是我的代码 <form class="form-wrap mt-4"> <div cla

在我的rails应用程序中,我尝试从头开始进行基于位置的搜索,我有三个字段用于位置状态、城市和区域。在州有许多城市的地方,城市属于一个州,城市有许多地区,地区属于一个城市

它的工作完全好,我现在用户可以选择州,城市和地区,并可以完成搜索,但我想要的是,如果用户登录并选择一个州,城市和地区,它应该被保存在他的帐户作为默认位置,因此,即使他注销并再次登录,他不应该再添加他的州,城市和地区

这是我的代码

<form class="form-wrap mt-4">
  <div class="btn-group" role="group" aria-label="Basic example">
    <input type="text" placeholder="What are your looking for?" id="search" class="btn-group1">

    <div class="select-wapper">
      <%= select_tag :state, options_for_select([["Select a state",""]] + State.all.map { |c| [c.name, c.id] } ), id: "state"%>
    </div>
    <div class="select-wapper">
      <%= select_tag :city, options_for_select([["Select a City",""]]), :id => 'city' %>
    </div>
    <div class="select-wapper">
      <%= select_tag :area, options_for_select([["Select a Area",""]]), :id => 'area' %>
    </div>
    <a href="/welcome/search" class="btn-form search-btn">SEARCH<i class="pe-7s-angle-right"></i></a>
  </div>
</form>
我的欢迎\u控制器(在应用@cnnr的答案后)

应用@cnnr answer后,我在搜索时出现此错误

undefined method `handle_search_params' for #<WelcomeController:0x00007f5a752e1950>
的未定义方法“handle\u search\u params”#

我希望这是足够的信息。我对rails非常陌生,我搜索了很多,但没有得到任何结果。

您可以将用户的搜索参数保存在
users
表中。 例如,首先,将搜索字段添加到
User
model:

class AddSearchFieldsForUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :search_state, :text
    add_column :users, :search_city, :text 
    add_column :users, :search_area, :text
  end
end
更新之后
WelcomeController

class WelcomeController < ApplicationController
  # `current_user` below is your authorized user
  # I assume, that you're using [devise][1] gem
  # because `current_user` helper belongs to it
  # If you're getting user instance otherwise
  # you should use your solution
  def search
    # I've just copy-paste your code with database querying
    # It should be optimized with scopes at model level,
    # but this is not the main question
    @products = Product.where('(name LIKE ? OR description LIKE ? )', "%#{params[:search]}%", "%#{params[:search]}%")
    @products = @products.where(state: params[:state]) if params[:state].present?
    @products = @products.where(city: params[:city]) if params[:city].present?

    if current_user
      current_user.update_attributes(
        search_state: params[:state],
        search_city: params[:city],
        search_area: params[:area]
      )
    end

    respond_with @products
  end

  ...
end
class WelcomeController
在您的搜索表中(我已经为简短的内容剥去了包装):


...
'城市'>
'区域'>
...
这是非常简单的解释,如果你需要更多的细节,我准备更新我的答案


链接到(不在代码块解析)

您可以将用户的搜索参数保存在
用户表中。
例如,首先,将搜索字段添加到
User
model:

class AddSearchFieldsForUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :search_state, :text
    add_column :users, :search_city, :text 
    add_column :users, :search_area, :text
  end
end
更新之后
WelcomeController

class WelcomeController < ApplicationController
  # `current_user` below is your authorized user
  # I assume, that you're using [devise][1] gem
  # because `current_user` helper belongs to it
  # If you're getting user instance otherwise
  # you should use your solution
  def search
    # I've just copy-paste your code with database querying
    # It should be optimized with scopes at model level,
    # but this is not the main question
    @products = Product.where('(name LIKE ? OR description LIKE ? )', "%#{params[:search]}%", "%#{params[:search]}%")
    @products = @products.where(state: params[:state]) if params[:state].present?
    @products = @products.where(city: params[:city]) if params[:city].present?

    if current_user
      current_user.update_attributes(
        search_state: params[:state],
        search_city: params[:city],
        search_area: params[:area]
      )
    end

    respond_with @products
  end

  ...
end
class WelcomeController
在您的搜索表中(我已经为简短的内容剥去了包装):


...
'城市'>
'区域'>
...
这是非常简单的解释,如果你需要更多的细节,我准备更新我的答案


链接到(代码块未解析)

hi@cnnr谢谢你的回答,我认为你的答案是正确的,但它不起作用,我用错误和我的控制器更新了我的问题,请看一看,这将是一个很大的帮助。再次感谢。您好@cnnr谢谢您的回答,我认为您的回答是正确的,但它不起作用,我已经用错误和控制器更新了我的问题,请看一下,这将是一个很大的帮助。再次感谢。
class WelcomeController < ApplicationController
  # `current_user` below is your authorized user
  # I assume, that you're using [devise][1] gem
  # because `current_user` helper belongs to it
  # If you're getting user instance otherwise
  # you should use your solution
  def search
    # I've just copy-paste your code with database querying
    # It should be optimized with scopes at model level,
    # but this is not the main question
    @products = Product.where('(name LIKE ? OR description LIKE ? )', "%#{params[:search]}%", "%#{params[:search]}%")
    @products = @products.where(state: params[:state]) if params[:state].present?
    @products = @products.where(city: params[:city]) if params[:city].present?

    if current_user
      current_user.update_attributes(
        search_state: params[:state],
        search_city: params[:city],
        search_area: params[:area]
      )
    end

    respond_with @products
  end

  ...
end
<form class="form-wrap mt-4">
    ...
    <input type="text" placeholder="What are your looking for?" id="search" class="btn-group1">
    <%= select_tag :state,
        options_for_select([["Select a state",""]] + State.all.map { |c| [c.name, c.id] }, current_user.search_state ), id: "state"%>
    <%= select_tag :city,
        options_for_select([["Select a City",""]], current_user.search_city), :id => 'city' %>
    <%= select_tag :area,
        options_for_select([["Select a Area",""]], current_user.search_area), :id => 'area' %>
    ...
</form>