Ruby on rails rails 4:使用自定义路由创建自定义搜索表单

Ruby on rails rails 4:使用自定义路由创建自定义搜索表单,ruby-on-rails,ruby,ruby-on-rails-4,routing,Ruby On Rails,Ruby,Ruby On Rails 4,Routing,我想这应该是一个容易的问题,但我现在就是想不出来。整天都在编码,需要另一双眼睛 正在显示当前URL: http://localhost:3000/q?utf8=✓&query%5Bquery%5D=testing 相对于 http://localhost:3000/q?query=testing routes.rb Rails.application.routes.draw do resources :biz do member do get 'photo'

我想这应该是一个容易的问题,但我现在就是想不出来。整天都在编码,需要另一双眼睛

正在显示当前URL:

http://localhost:3000/q?utf8=✓&query%5Bquery%5D=testing
相对于

http://localhost:3000/q?query=testing
routes.rb

Rails.application.routes.draw do

  resources :biz do
    member do
      get 'photo'
    end
  end
  get '/q' => 'search#index', as: :search


  root 'welcome#index'

end
class SearchController < ApplicationController
  def index
    if params.has_key?(:query)
      @query = Model.q(params[:query])
    else
      redirect_to root_path
    end
  end
end
欢迎/index.haml

= simple_form_for :query, url: search_path, method: "get" do |q|
  = q.input :query
我有一个搜索\u控制器.rb

Rails.application.routes.draw do

  resources :biz do
    member do
      get 'photo'
    end
  end
  get '/q' => 'search#index', as: :search


  root 'welcome#index'

end
class SearchController < ApplicationController
  def index
    if params.has_key?(:query)
      @query = Model.q(params[:query])
    else
      redirect_to root_path
    end
  end
end
class SearchController
我需要修复或添加什么

检查员的表格

<form novalidate="novalidate" class="simple_form /q" action="/q" accept-charset="UTF-8" method="get">
  <input name="utf8" type="hidden" value="✓">        
  <div class="form-group string required query_query">
    <label class="string required control-label" for="query_query">
      <abbr title="required"></abbr> Query
    </label>
  <input class="string required form-control" type="text" name="query[query]" id="query_query">      
  </div>           
</form>

查询

删除/:查询,因为它是作为查询参数而不是路径参数发送的。 使其成为
/q

另外,形式可能必须是

=simple_form_for:search,url:search_path do | q |

表格将不提交

URL问题


问题是简单的表单正在将其包装到模型中。因此,对于输入,您的参数将是
params[:query][:query]
。您可以只使用
text\u field\u标记“query”
而不是
q。输入:query

删除/:查询,因为它是作为查询参数而不是路径参数发送的。 使其成为
/q

另外,形式可能必须是

=simple_form_for:search,url:search_path do | q |

表格将不提交

URL问题


问题是简单的表单正在将其包装到模型中。因此,对于输入,您的参数将是
params[:query][:query]
。您可以只使用
text\u field\u标记“query”
而不是
q。输入:query

仍然获得无路由匹配。我删除了/:查询。此错误:
没有路径匹配[POST]“/”
搜索路径的简单表单,方法:“get”do | q |
表单默认为POST,而您的路径为get。我越来越近了!没有错误,但它不会转到搜索页面,它会停留在欢迎页面(输入字段所在的位置),除了url之外,什么都不会发生,它会更改为:
http://localhost:3000/?utf8=✓&%2Fb%5Bquery%5D=在我进行类型测试时进行测试
。它不会转到
http://localhost:3000/q?query=testing
如您所述,现在可以粘贴完整的路线定义了吗?看不到你面前有什么。并检查页面上的表单,告诉我们标签显示了什么。如果您用修订版更新了我的答案,可能在获得无路线匹配之前您没有执行/QS。我删除了/:查询。此错误:
没有路径匹配[POST]“/”
搜索路径的简单表单,方法:“get”do | q |表单默认为POST,而您的路径为get。我越来越近了!没有错误,但它不会转到搜索页面,它会停留在欢迎页面(输入字段所在的位置),除了url之外,什么都不会发生,它会更改为:
http://localhost:3000/?utf8=✓&%2Fb%5Bquery%5D=在我进行类型测试时进行测试
。它不会转到
http://localhost:3000/q?query=testing
如您所述,现在可以粘贴完整的路线定义了吗?看不到你面前有什么。检查页面上的表格,告诉我们标签上显示了什么。如果你用修订版更新了我的答案,可能你没有/q