简体   繁体   English

Rails 4搜索Multiple Params

[英]Rails 4 search Multiple Params

I'm looking to search and find results if two params exist, but i'm getting sent to car_show_path, but should have results. 如果存在两个参数,我正在寻找搜索和查找结果,但我将被发送到car_show_path,但应该有结果。

Model 模型

class Car < ActiveRecord::Base
  def self.search(car_number, car_model) 
    where(['car_number = ? AND car_model = ?', "%#{car_number}%", "%#{car_model}%"])
  end
end

Controller Show 控制器展示

 @search = Car.search(params[:car_number], params[:car_model])
   if @search.present?
       @search
    else
     redirect_to car_path, notice: "Not a valid combination"
    end

Form 形成

<%= simple_form_for :search, url: car_show_path do |f| %>
  <%= f.input :car_number, :collection => @car.collect {|c| [c.number]}, :include_blank => false %>
  <%= f.input :car_model, placeholder: "Car Model" %>
  <%= f.button :submit, 'Generate', class: 'btn' %>
<% end %>

You are doing it wrong. 你做错了。 If you look into the params hash generated in the server log, you can see something like this :search => {:car_model => "value", :car_number => "Value"} . 如果查看服务器日志中生成的params哈希,可以看到如下内容:search => {:car_model => "value", :car_number => "Value"} That means the values of :car_model and :car_number cannot be retrieved with params[:car_model] and params[:car_number] , instead you should use params[:search][:car_model] and params[:search][:car_number] 这意味着:car_model:car_number值不能用params[:car_model]params[:car_number]检索,而应该使用params[:search][:car_model]params[:search][:car_number]

@search = Car.search(params[:search][:car_number], params[:search][:car_model])
if @search.present?
  @search
else
  redirect_to car_path, notice: "Not a valid combination"
end

I see the code you have provided. 我看到了你提供的代码。 I believe the code you have written need some improvement. 我相信你写的代码需要一些改进。 So I have re-written code which may solve your issue and bring to your goal. 所以我重新编写了可以解决您的问题并实现目标的代码。 Here it is : 这里是 :

#/app/models/car.rb

class Car < ActiveRecord::Base
  def self.search(cn, cm) 
    where('car_number = ? AND car_model = ?', cn, cm)
  end
end

#/app/controllers/cars_controller.rb

class CarsController < ApplicationController

  def search
    @result = Car.search params[c_number], params[c_modal]
  end
end


#/app/views/cars/search.html.erb
#you can generate similar form with simple_form_for

<form action="/search" method="/get" >
  <input type="text" name="c_number"
  <input type="text" name="c_modal">
  <input type="submit" value="search">
</form>

<% if !@result.any? %>
  Not a valid combination
<% end %>

<% @result.each do |r|%>
  <%= r.car_number %>
  <%= r.car_modal %>
<% end %>


#/config/routes.rb

get "/search" => "cars#search"

Note : Above code is best of my practice and didn't executed locally. 注意:上面的代码是我的最佳实践,并没有在本地执行。

Hope that helps!!! 希望有所帮助!

You are putting the same values into both placeholders from the query parameter that is being sent to your search method. 您将相同的值放入从发送到搜索方法的查询参数的两个占位符中。 That doesn't seem right. 这似乎不对。

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

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