简体   繁体   English

从Rails中的表单参数生成URL

[英]Generating URLs from form params in Rails

Is it possible to generate the url for the form submit based off of the values in the form? 是否可以根据表单中的值生成表单提交的URL? I am trying to create a search form that asks for the make and model 我正在尝试创建一个要求制作和模型的搜索表单

<% form_tag make_model_search_path(:client_id => @client.id) do %>
<%= select_tag :make, options_for_select(make_list) %>
<%= select_tag :model, options_for_select(model_list) %>
<%= submit_tag "Search" %>
<% end %>

The route I want to use is the following to display the results 我想要使​​用的路线如下显示结果

map.make_model_search "client/:client_id/tags/search/make/:make/model/:model", :controller => "tags", :action => "make_model_search", :method => :get

But since the URL is generated before any parameters are sent I do not know how to do this 但由于URL是在发送任何参数之前生成的,因此我不知道如何执行此操作

The reason I want it to be done this way is because I need to add pagination to this eventually by appending "page/:page" to this route. 我希望以这种方式完成的原因是因为我需要通过在此路由中附加“page /:page”来为此添加分页。

If this is not possible. 如果这是不可能的。 I also do not understand why the params are not in the the url as 我也不明白为什么params不在url中

"?client_id=ID&make=MAKE&model=MODEL"

It it my understanding that this will allow me to do the same thing 我的理解是,这将允许我做同样的事情

Thank YOU! 谢谢!

I also do not understand why the params are not in the the url as 我也不明白为什么params不在url中

"?client_id=ID&make=MAKE&model=MODEL" “?CLIENT_ID = ID&使= MAKE型号= MODEL”

It it my understanding that this will allow me to do the same thing 我的理解是,这将允许我做同样的事情

From the documentation for form_tag : form_tag的文档:

The method for the form defaults to POST. 表单的方法默认为POST。

You need to add :method => "get" to your form_tag options if you want to have the options passed in the query string. 如果要在查询字符串中传递选项,则需要在form_tag选项中添加:method =>“get”。

Using the "client/:client_id/tags/search/" is mostly the REST approach that Rails uses for working with your routes. 使用“client /:client_id / tags / search /”主要是Rails用于处理路由的REST方法。 In that view, you are accessing representations of resources (say, a client). 在该视图中,您正在访问资源的表示(例如,客户端)。

The "?client_id=ID" way is just sending some params. “?client_id = ID”方式只是发送了一些参数。

So, in the first case, you are explicitly asking for a representation of a client, while on the second one you are never sure what's going on. 因此,在第一种情况下,您明确要求表示客户端,而在第二种情况下,您永远不会确定发生了什么。 It's a simpler way of expressing and requesting information. 这是一种表达和请求信息的简单方法。

Now, in your question, I think you are talking about the second case. 现在,在你的问题中,我认为你在谈论第二个案例。 If you do want to append the params using a GET statement (since the form will default to POST), you can add a method param: 如果你想使用GET语句附加params(因为表单默认为POST),你可以添加方法参数:

form_tag make_model_search_path(:client_id => @client.id), :method => "get" 

As a side note, I think you are over complicating your route. 作为旁注,我认为你的路线过于复杂。 If you just want to use params, you could just work with something like 如果你只想使用params,你可以使用类似的东西

"client/:client_id/search/", :controller => "tags", :action => "make_model_search", :method => :get

Your params (:model, :make) will be available in your controllers and if you want to use pagination (checkout the will_paginate plugin) it will work fine. 您的params(:model,:make)将在您的控制器中可用,如果您想使用分页(检查will_paginate插件),它将正常工作。

Good luck! 祝好运!

BTW, the routes are not actually generated before. 顺便说一句,之前并没有真正生成路线。 The "/:client_id/", "/:make" and others act as placeholders that the route engine will use to check if some route responds to a typed URL and execute it after assigning the variables according. “/:client_id /”,“/:make”和其他人充当占位符,路由引擎将使用它来检查某个路由是否响应键入的URL并在分配变量后执行它。

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

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