加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby on Rails:如何使用link_to将参数从视图

发布时间:2020-12-16 20:13:15 所属栏目:百科 来源:网络整理
导读:我目前在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中使用一个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将被调用.
这是一切正常,但网址显示所有的参数

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上吗?

解决方法

也许您可以对参数进行编码并在控制器中对其进行解码,以阻止可能需要修改URL的用户?可能会过分杀人,但…
>> author=ActiveSupport::Base64.encode64("author=jim")
=> "YXV0aG9yPWppbQ==n"
>> ActiveSupport::Base64.decode64(author)
=> "author=jim"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读