使用带有参数和 form_tag 的命名路由

发布于 2024-08-19 16:17:45 字数 1177 浏览 5 评论 0原文

我正在尝试在 Rails 中创建一个简单的搜索表单,但我认为我遗漏了一些东西。

我有一个用于搜索的命名路线:

map.search ":first_name/:last_name", :controller => "home", :action => "search"

我试图在我的搜索表单中使用它:

<% form_tag(search_path, :method => 'get') do %>
  <%= text_field_tag(:first_name) %>
  <%= text_field_tag(:last_name) %>
  <%= submit_tag("Search") %>
<% end %>

但是当我加载搜索表单时,我得到一个 ActionController::RoutingError:

无法从 {:action=>"search", :controller=>"home"} 生成 search_url - 您可能有不明确的路线,或者您可能需要为此路线提供其他参数。 content_url 具有以下必需参数: [:first_name, :last_name] - 他们都满意吗?

我缺少什么?我认为表单中定义的字段会自动与我的路线参数链接。 :-/

更新:

据我了解,search_path 是在现在显示表单之前生成的,因此无法更新。事后看来很明显!

我改变了我的路线:

map.search 'search', :controller => "home", :action => "search"
map.name ':first_name/:last_name', :controller => "home", :action => "name"

现在 search 操作就可以了:

def search
  redirect_to name_path(params)
end

一切都很顺利!这里的主要目标是从 name 命名路由中获取该 URL 作为搜索结果。谢谢你们!

I'm trying to create a simple search form in Rails, but I think I'm missing something.

I have a named route for search:

map.search ":first_name/:last_name", :controller => "home", :action => "search"

I'm trying to use that in my search form:

<% form_tag(search_path, :method => 'get') do %>
  <%= text_field_tag(:first_name) %>
  <%= text_field_tag(:last_name) %>
  <%= submit_tag("Search") %>
<% end %>

But when I load up the search form I get an ActionController::RoutingError:

search_url failed to generate from {:action=>"search", :controller=>"home"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: [:first_name, :last_name] - are they all satisfied?

What am I missing? I thought the fields defined in my form would be automatically linked up with my route parameters. :-/

Update:

I understand that search_path is generated before the form is displayed now, so it can't be updated. Obvious in hindsight!

I changed my routes:

map.search 'search', :controller => "home", :action => "search"
map.name ':first_name/:last_name', :controller => "home", :action => "name"

And now the search action just does:

def search
  redirect_to name_path(params)
end

It all works a treat! The main goal here was getting that URL from the name named route as result of doing a search. Thanks guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福丶如此 2024-08-26 16:17:45

form_for 生成表单,它必须指定创建 search_path 所需的所有参数,因此它应该如下所示:

<% form_tag(search_path, :firstname => 'some_text', :lastname => 'some_text', :method => 'get') do %>

或者至少是这样的。 HTML 标签 form 具有参数 action='/some/url',这就是为什么您必须为 search_path 指定所有参数。但上面的例子不会像你预期的那样工作。

那么你能做什么呢?

  1. 创建具有 action='/' 的空表单,并在提交之前使用 js 将其替换为输入文本字段的内容。

  2. 创建另一个路由,例如 /search,从提交接收参数,然后重定向到正确的路径。

可能还有一些更好的方法来做到这一点;)

form_for generates form and it has to have specified all parameters that are needed to create search_path, so it should look like:

<% form_tag(search_path, :firstname => 'some_text', :lastname => 'some_text', :method => 'get') do %>

or at least something like this. HTML tag form has parameter action='/some/url' and that's why you have to specify all parameters for search_path. But the above example won't work as you expected.

So what you can do?

  1. Create empty form that has action='/' and with js replace it with content of your input text fields before submitting.

  2. Create another route, on example /search that recives parameters from submit and then redirects to correct path.

Probably there is also some better ways to do it ;)

叫嚣ゝ 2024-08-26 16:17:45

首先,search_path 实际上是一个方法,它采用选项的哈希值。这个方法应该接收 :first_name:last_name

其次,浏览器只能将表单参数作为 POST 请求的正文或作为查询字符串参数(对于任何类型的请求方法)提交。因此,不幸的是,浏览器的本机提交功能无法生成这种 URL。

另一种思考方式:您在这里所做的就是用 URL 填充表单标记的 action 属性。当您构建表单时,Rails 需要完整的 URL。因此,需要在调用表单助手时指定路由中的所有参数,而不是在下一个 POST 请求时指定。

不幸的是,您想要做的事情在普通的 Rails 应用程序中是不可能的。

(如果你真的愿意,你可以通过编写自己的表单助手和一些 Javascript 来替换浏览器的本机提交功能来实现它。然后 Javascript 会根据表单字段构造该 URL。我会但反对它。)

First, search_path is actually a method, which takes a hash of options. It is this method which should receive :first_name and :last_name.

Second, a browser can only submit form parameters as the body of a POST request, or as query string parameters (for any kind of request method). So there's unfortunately no way a browser's native submit function can generate that kind of URL.

Another way of thinking of it: What you're doing here is filling the form tag's action attribute with an URL. Rails needs a complete URL as you're building the form. So all parameters in your route need to be specified when the form helper is called, rather than at the next POST request.

So unfortunately, what you're trying to do is not possible in a normal Rails application.

(If you really want to, you might be able to pull it off by writing your own form helpers and a bit of Javascript to replace the browser's native submit function. The Javascript would then construct that URL based on the form fields. I'd argue against it, though.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文