Ruby on Rails:如何使用 link_to 将参数从视图传递到控制器,而 URL 中不显示参数 [英] Ruby on Rails: How to pass parameters from view to controller with link_to without parameters showing up in URL

查看:24
本文介绍了Ruby on Rails:如何使用 link_to 将参数从视图传递到控制器,而 URL 中不显示参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 将参数分配给一个对象,以便稍后在 View 中的表单使用(在 new.html.erb 中)

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 然后将被调用.这一切正常,但 url 显示了所有参数

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

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

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

推荐答案

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

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"

这篇关于Ruby on Rails:如何使用 link_to 将参数从视图传递到控制器,而 URL 中不显示参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆