如何在Rails中将字段设置为默认值

时间:2019-06-14 13:34:32

标签: ruby-on-rails

在我的Rails应用程序中,我试图从头开始进行基于位置的搜索,我为位置State,City和Area设置了三个字段。在州有许多城市的地方,城市属于一个州,城市有许多区域,而区域属于一个城市。

它对我来说完全正常,现在用户可以选择州,城市和区域并可以完成搜索,但是我想要的是,如果用户登录并选择州,城市和区域,则应该将其保存在他的帐户作为默认位置,这样,即使他注销并再次登录,也不必再次添加州,城市和地区。

这是我的代码

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

我的welcome_controller (在应用@cnnr的答案之前)

  def search
  @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?
  end

我的welcome_controller (应用@cnnr的答案后。)

  def search
  @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?


  search_results = handle_search_params(params[:search])

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

    respond_with(search_results) 
  end
end

应用@cnnr答案后,搜索时出现此错误

undefined method `handle_search_params' for #<WelcomeController:0x00007f5a752e1950>

我希望这是足够的信息。铁杆真的很陌生,我已经搜索了很多,但没有任何结果。

1 个答案:

答案 0 :(得分:0)

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

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

然后运行

$ bundle exec rake db:migrate

此更新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

在您的搜索表单中(为简便起见,我使用了包装纸):

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

这是非常简单的解释,如果您需要更多详细信息,我准备更新我的答案。

链接到devise(在代码块中未解析)