【问题标题】:Form tag not routing correctly on Rails5表单标签在 Rails5 上没有正确路由
【发布时间】:2019-11-02 06:13:56
【问题描述】:

我想做的事:

在 Rails 上实现搜索表单。 如果用户单击搜索按钮,rails 将从文本输入中读取值并将其作为查询传递。 当我点击按钮时,它应该转到 localhost/internships/search/keyword?keyword=blahblah

出了什么问题

但每当我点击按钮时,它都会转到 localhost?keyword=blahblah

我做了什么:

这是我的代码

search_controller.rb

<div class="input-group">
    <%= form_tag internships_keyword_search_path, method: :get do %>
        <%= text_field_tag :keyword, params[:keyword], placeholder: "Search query", class: "form-control" %>
        <%= submit_tag "search", name: nil, class: "btn btn-danger wrn-btn" } %>
    <% end %>
</div>

routes.rb

get '/internships/search/keyword', to: 'internships_search#search_keyword', as: 'internships_keyword_search'

更新

1。 search_keyword 的内容

def search_keyword
        @internships = Internship.where("subject LIKE :keyword OR content LIKE :keyword", keyword: params[:keyword]).all
        if @internships.length == 0 then
            render :empty
        else
            render :show
        end
end

2。当我将 form_tag 更改为 link_to 时,它正在工作。但 button_to 不是。

3。 nginx配置

upstream rails_app {
  server app:3000;
}

server {
  # define your domain
  server_name www.example.com;

  # define the public application root
  root   $RAILS_ROOT/public;
  index  index.html;

  # define where Nginx should write its logs
  # access_log $RAILS_ROOT/nginx.access.log;
  # error_log $RAILS_ROOT/nginx.error.log;

  # deny requests for files that should never be accessed
  location ~ /\. {
    deny all;
  }

  location ~* ^.+\.(rb|log)$ {
    deny all;
  }

  # serve static (compiled) assets directly if they exist (for rails production)
  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  # send non-static file requests to the app server
  location / {
    try_files $uri @rails;
  }

  location @rails {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails_app;
  }
}

【问题讨论】:

  • 如果您在 Rails 控制台中打印 app.internships_keyword_search_path,您会得到什么?
  • @Gokulp irb(main):001:0&gt; app.internships_keyword_search_path =&gt; "/internships/search/keyword"
  • 你可以试试form_tag internships_keyword_search_url这可能会有所帮助
  • @Gokulp 谢谢。不幸的是,它仍然无法正常工作。
  • 你能告诉我们你在internships_search_controller.rb中的search_keyword方法吗?

标签: ruby-on-rails ruby


【解决方案1】:

试试这个

 <%= form_tag("/internships/search/keyword", method: "get") do %>

如果这不起作用,请解释在您提交表单时会影响您操作的参数

【讨论】:

  • 它不起作用,我传递的唯一参数是 :keyword ,如上所示。
【解决方案2】:

自我回答:问题出在嵌套表单上。 我的 html.erb 的完整代码是

<form ...>
    <div class="input-group">
        <%= form_tag internships_keyword_search_path, method: :get do %>
            <%= text_field_tag :keyword, params[:keyword], placeholder: "Search query", class: "form-control" %>
            <%= submit_tag "search", name: nil, class: "btn btn-danger wrn-btn" } %>
        <% end %>
    </div>
</form>

form_tag 内部的外部表单标签块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 2020-07-29
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多