Html 从表单参数创建自定义url
我目前正在开发一个工具,它采用表单输入标记,并根据这些输入生成一个页面 我的表单如下所示:Html 从表单参数创建自定义url,html,ruby-on-rails,parameters,routes,Html,Ruby On Rails,Parameters,Routes,我目前正在开发一个工具,它采用表单输入标记,并根据这些输入生成一个页面 我的表单如下所示: <%= form_tag({:action => "show", :controller => "tool"}) do %> .... .... .... <% end %> 然后我使用show.html.erb来处理show方法。为了让这一切在我的生活中发挥作用 routes.rb: match "/tool" => "tool#index"
<%= form_tag({:action => "show", :controller => "tool"}) do %>
....
....
....
<% end %>
然后我使用show.html.erb来处理show方法。为了让这一切在我的生活中发挥作用
routes.rb:
match "/tool" => "tool#index"
match "/tool/show/" => "tool#show"
我希望url显示所有参数,如/tool/param1/param2/…
,而不仅仅是/tool/show/
。我尝试将以下内容添加到路线中
match "/tool/show/:param1/:param2" => "tool#show"
但是它会搞砸,因为我的表单只调用
show
。我是如何做到这一点的,即仅使用方法show
,创建一个与所有参数的链接,并在提交表单后生成参数的您需要用斜杠分隔参数吗,或者通常的方法是获取
参数吗?例如,像这样:
<%= link_to "link text", post_path(name: @post.name, email: @post.email, message: @post.message) %>
/posts/5?email=def&message=ghi&name=abc
您可以使用URL帮助器创建这样的链接,代码如下:
<%= link_to "link text", post_path(name: @post.name, email: @post.email, message: @post.message) %>
对不起,ruby是个新手,能告诉我上面的表单是如何使用表单标签的吗?我需要改变我的路线。我不知道你的意思。您的表单是否在new.html.erb
视图中?它在index.html.erb中,表单正在调用show,它使用show.html.erb。您的param1
、param2
等从何而来?值来自index.html.erb中的表单,然后在show方法中获取它们并处理show.html.erb