ruby-on-rails - 将搜索功能提取到 Rails 中的表单对象

标签 ruby-on-rails ruby ruby-on-rails-4

在我的 Rails 应用程序中,我有简单的搜索功能。我想提取到 Form Object 但不知道该怎么做。我有如下所示的搜索表单:

.row
  = horizontal_simple_form_for :cars, {url: cars_path, method: :get} do |f|
    .col-md-4
      .row
        .col-md-12
          = f.input :handover_location, label: I18n.t('.handover'), collection: Location.all.map{|hl| [hl.location_address, hl.id]}
          = f.input :return_location, label: I18n.t('.return') ,collection: Location.all.map{|rl| [rl.location_address, rl.id]}
      = f.input :car_class, label: I18n.t('.car_class') ,collection: CarClass.all.map { |c| [c.name, c.id] }, include_blank: true
    .col-md-4  
      = f.input :handover_date, as: :string, label: false
      = f.input :return_date, as: :string, label: false
      = f.submit class: 'btn btn-success' 

汽车 Controller :

class CarsController < ApplicationController
  skip_authorization_check

  def index
    @cars = Car.search(params)
  end

  def show
  end
end

Car 模型中搜索正确汽车的类方法:

 def self.search(params) 
    self.joins(:reservations).where.not("reservations.reception_time <= ? AND reservations.return_time >= ?", 
        params[:cars][:return_date], params[:cars][:handover_date]).
        joins(:car_class).where("car_classes.id= ?", params[:cars][:car_class])
        .cars_at_both_locations(params[:cars][:handover_location], params[:cars][:return_location])
  end

现在我试图将其提取到表单对象中。我创建了一个文件 search_form.rb:

class SearchForm
  include ActiveModel::Model
  attr_accessor :handover_date, :return_date, :handover_location, :return_location, :car_class
end

但是现在我不知道如何处理这个表单对象的参数。提前致谢。

最佳答案

我希望我能在 Form Object 方面帮助您,但我需要了解更多关于 classes & modules


可以帮助您使用搜索功能,正如我们之前所做的那样here

enter image description here

这是我们使用的代码:

#View
<%= form_tag search_path, :method => :post, :id => "SearchForm" do %>
    <%= text_field_tag :search, params[:search], placeholder: 'Search your favourite products or brands', :autocomplete => :off  %> 
    <%= image_submit_tag 'nav_bar/search.png' %>
<% end %>   

#config/routes.rb
match 'search(/:search)', :to => 'products#search', :as => :search, via: [:get, :post]

#app/controllers/products_controller.rb
def search
    @products = Product.search(params[:search])
    respond_to do |format|
        format.js     { render :partial => "elements/livesearch", :locals => {:search => @products, :query => params[:search]} }
        format.html   {  render :index }
    end
end

注意 form_tag我们用过?

Simple form当前不适用于 form_tag(它需要一个对象)- 我们只是将数据与 GET 请求一起发送到 Controller ,然后 Controller 将数据发送到 产品型号

我认为您的问题是由使用SearchForm 对象引起的。您只需要这个,因为您使用 简单形式 意味着您必须传递一个对象。问题是这不是搜索所必需的

更好的方法是使用标准的 form_tag,并将请求直接发送到您的 Controller 。这将允许您将数据作为 params 进行处理,您可以将其直接发送到您的 Car 模型

--

如果你愿意,我可以写一些特定于你的代码

关于ruby-on-rails - 将搜索功能提取到 Rails 中的表单对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755499/

相关文章:

ruby-on-rails - 停止 AASM 过渡的最佳方法是什么

ruby-on-rails - 无法测试应该属于_to,在 Rails 上缺少 id 外键

ruby-on-rails - 自定义子域上的 Rails 命名空间管理

ruby - 冒号的意义 : in Ruby

sql - Rails 4 按 has_many 计数排序

ruby-on-rails - 无法找到 json 的输入类 - 在 Active Admin 中处理 JSON 类型

ruby-on-rails - Rails 获取当前登录的 Windows 用户的用户名

ruby - 无法使用 Watir 绕过 Firefox 的不安全证书警告

ruby-on-rails - rails : Multiple parameters before do?

javascript - rails : Why can't my object created by ajax be used to trigger javascript?