Ruby on Rails: How to pass parameters from view to controller with link_to without parameters showing up in URL

user1994764 picture user1994764 · Sep 7, 2010 · Viewed 19.9k times · Source

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?

Answer

Danny picture Danny · Dec 13, 2010

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"