Ruby on Rails:如何使用 link_to 将参数从视图传递到控制器,而不需要在 URL 中显示参数

发布于 2024-09-18 06:16:27 字数 1143 浏览 9 评论 0原文

我目前在 View 中使用 link_to 帮助器将 title 、author 、image_url 和 isbn 等参数传递回控制器,

<%= link_to 'Sell this item',new_item_path(:title => title, :author => authors, :image_url=>image, :image_url_s=>image_s, :isbn=>isbn, :isbn13=>isbn13 ) %>

然后控制器将这些参数分配给稍后在 View 中供表单使用的对象(在 new.html.erb 中)

def new
      @item = Item.new

      @item.title = params[:title]
      @item.author = params[:author]
      @item.image_url = params[:image_url]
      @item.image_url_s = params[:image_url_s]
      @item.isbn = params[:isbn]
      @item.isbn13 = params[:isbn13]

      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @item }
      end
end

new然后将调用 .html.erb。 这一切都工作正常,但 url 显示了所有参数

http://localhost:3000/items/new?author=Michael+Harvey&image_url=http://ecx.images-amazon.com/images/I/51vt1uVjvLL._SL160_.jpg&image_url_s=http://ecx.images-amazon.com/images/I/51vt1uVjvLL._SL75_.jpg&isbn13=9780307272508&isbn=0307272508&title=The+Third+Rail

有什么办法可以让参数不显示在 URL 上吗?

I am currently using a link_to helper in View to pass parameters like title , author ,image_url and isbn back to controller

<%= link_to 'Sell this item',new_item_path(:title => title, :author => authors, :image_url=>image, :image_url_s=>image_s, :isbn=>isbn, :isbn13=>isbn13 ) %>

Controller will then assign the parameters to an object to be used by a form in View later(in new.html.erb)

def new
      @item = Item.new

      @item.title = params[:title]
      @item.author = params[:author]
      @item.image_url = params[:image_url]
      @item.image_url_s = params[:image_url_s]
      @item.isbn = params[:isbn]
      @item.isbn13 = params[:isbn13]

      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @item }
      end
end

new.html.erb will then be called.
This is all working fine but the url shows all the parameters

http://localhost:3000/items/new?author=Michael+Harvey&image_url=http://ecx.images-amazon.com/images/I/51vt1uVjvLL._SL160_.jpg&image_url_s=http://ecx.images-amazon.com/images/I/51vt1uVjvLL._SL75_.jpg&isbn13=9780307272508&isbn=0307272508&title=The+Third+Rail

Is there any way I can make the parameters not show up on the URL?

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

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

发布评论

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

评论(4

缘字诀 2024-09-25 06:16:27

也许您可以对参数进行编码并在控制器中对其进行解码,以阻止可能想要修改 url 的用户?可能有点矫枉过正,但是……

>> author=ActiveSupport::Base64.encode64("author=jim")
=> "YXV0aG9yPWppbQ==\n"
>> ActiveSupport::Base64.decode64(author)
=> "author=jim"

Maybe you could encode the parameters and decode them in the controller to deter users who may want to modify the url? Might be overkill but...

>> author=ActiveSupport::Base64.encode64("author=jim")
=> "YXV0aG9yPWppbQ==\n"
>> ActiveSupport::Base64.decode64(author)
=> "author=jim"
墨落成白 2024-09-25 06:16:27

POST 可用于将参数从 URL 移出并移入请求,但这不是“正确”或最佳实践。 HTTP 标准规定,非 GET 请求仅用于更改服务器状态的请求。这就是为什么当您刷新响应 POST 生成的页面时会收到警告的原因。

在 URL 中包含参数并没有什么问题。不应过多关注 URL 栏上显示的内容,更不用说 ? 后面的内容了。然而,如果您有某种需要(即客户的坚持)删除它们,您有多种选择,约翰提到了其中两个。

我假设您的“新”操作是 REST 风格的,因为它正在生成一个表单,必须提交该表单才能更改服务器上的状态。因此,您的选择可能是:

  1. 使用 POST,即使它不符合标准。不推荐。
  2. 使用 AJAX 获取。这需要 javascript,而 ajax 处理确实增加了 JS 框架的使用和测试等要求。
  3. 使用 GET(或 POST),但捕获参数并存储它们,将用户重定向回另一个显示这些存储值的干净 URL。您可以将它们存储在会话哈希中,或者创建它们的数据库记录。实际上,在这种情况下您确实应该使用 POST,因为您可以通过存储这些参数来有效地更改服务器上的状态。在这种情况下,如果用户刷新他所定向的页面,这些参数将被保留。这有效地消除了刷新时的浏览器警告,我当然可以欣赏这一点。

A POST can be used to move the parameters out of the URL and into the request, but this is not the "correct" or best practice. HTTP standards are such that non-GET requests are meant to be used only for requests that change state on the server. This is why you get a warning when you refresh a page that was generated in response to a POST.

There is nothing wrong with having parameters in the URL. So much focus should not be made on what appears to the URL bar, let alone what's after the ?. If however you have some need (i.e. insistence of a client) to remove them, you have several options, two of which John mentions.

I'm assuming your "new" action is REST-style, in that it's generating a form that would have to be submitted to change state on the server. Therefore your options might be:

  1. Use POST, even though it's not standard compliant. Not recommended.
  2. Use AJAX GET. This requires javascript, and ajax handling does add requirements such as the use of a JS framework and testing.
  3. Use GET (or POST), but capture the parameters and store them, the redirect the user back to another clean URL that displays those stored value. You could store those in the session hash, or create a database record of them. Actually you really should use POST in this case, since you are effectively changing state on the server by storing those parameters. In this case, if the user refreshes the page he is directed to, those parameters will be preserved. This effectively removes the browser warning on refresh, something I can certainly appreciate.
煮茶煮酒煮时光 2024-09-25 06:16:27

我可以看到两个选项,并且都涉及 JavaScript:

  • 让链接填充参数的隐藏表单字段,然后使用 HTTP POST 请求提交表单
  • 让链接向控制器操作提交 AJAX 请求(使用 HTTP GET,除非单击链接会更改服务器端状态,在这种情况下应使用 POST)

我想我会采用第二种方法。

There are two options that I can see and both involve JavaScript:

  • Have the link populate hidden form fields for the parameters and then submit the form using an HTTP POST request
  • Have the link submit an AJAX request to the controller action (using an HTTP GET unless clicking the link changes server-side state, in which case a POST should be used)

I think I would go with the second approach.

最终幸福 2024-09-25 06:16:27

为什么不将它们写入会话呢?看起来您的数据可能少于 4k。只要记得擦拭即可。

Why not write them to the session? It looks like you might have less than 4k in data there. Just remember to wipe it.

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