【问题标题】:How to get a search form to actually perform a search query (in Rails)如何让搜索表单实际执行搜索查询(在 Rails 中)
【发布时间】:2014-04-05 14:12:26
【问题描述】:

我有一个从this tutorial 创建的搜索表单。但是如何让它对指定的控制器和指定的操作执行实际查询呢?

%form
  %input{:placeholder => "Find something...", :required => "", :type => "text"}
    %button{:type => "submit"} Search

【问题讨论】:

    标签: html ruby-on-rails search ruby-on-rails-4 haml


    【解决方案1】:

    你需要为表单设置一个目标:

    %form{ :action => "/rotas", :method => "get" }
      %input{:placeholder => "Find something...", :required => "", :type => "text"}
        %button{:type => "submit"} Search
    

    或者您可以使用 form_tag helper ,例如:

    =form_tag(controller: :posts , action: :index, method: :get) do |f|
      = f.text_field :query, nil, :placeholder => "Find something..."
        = f.submit "Search"
    

    记得留意indentations

    【讨论】:

    • 感谢@Nimir,我可以看到第一个建议在没有弄乱样式的情况下有效。但是如何将搜索文本放入查询中?当我提交它时,它只会发送到Started POST "/rotas" for 127.0.0.1 at 2014-04-05 16:33:13 +0200
    • /rotas 是一个例子,你应该用你想要接收搜索查询的路径替换它
    • 是的,我明白了,但假设我有 /rotas 路径,我仍然需要我的查询在参数中。就像例如/rotas?query=mysearch.
    • @JohnSmith1976 也许你应该通过guides.rubyonrails.org/form_helpers.html 它包含构建搜索表单的确切示例
    • @JohnSmith1976 尝试使用:method => "get"
    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 2012-06-25
    • 2010-11-12
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2015-06-26
    相关资源
    最近更新 更多