gpt4 book ai didi

ruby-on-rails - link_to 将参数与 url 一起发送并在目标页面上抓取它们

转载 作者:行者123 更新时间:2023-12-01 04:49:09 25 4
gpt4 key购买 nike

我如何在页面上建立一个链接,将用户带到另一个 URL 并传递一个参数,我们如何在目标 url 上获取该参数。

通常我会添加如下链接:

 <%= link_to "Add Product", '/pages/product' %>

但是如何将参数与此 url 一起发送?我可以通过 using params[:parm_name] 在目标操作中选择它们吗?

最佳答案

只需将它们添加到链接:

<%= link_to "Add Product", '/pages/product?param1=value1&param2=value2' %>

并在 Controller 中:
param1 = params[:param1] # "value1"
param2 = params[:param2] # "value2"

如果你对路由使用辅助方法(例如 company_path ),那么你可以添加参数的哈希值,所以这两个应该是相似的:
<%= link_to "Add Product", new_product_path(:param1 => "value1", :param2 => "value2") %>
<%= link_to "Add Product", "/products/new?param1=value1&param2=value2" %>

来自 documentation :
link_to "Comment wall", profile_path(@profile, :anchor => "wall")
# => <a href="/profiles/1#wall">Comment wall</a>

link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails"
# => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>

link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
# => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>

关于ruby-on-rails - link_to 将参数与 url 一起发送并在目标页面上抓取它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2124862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com