{ :co" /> { :co" /> { :co"/>

Ruby on Rails - Passing a hash as a parameter with link_to_remote Rails 2

2.1k Views Asked by At

am working in Rails version 2.3.8 Hash => agent_list = [[5, "val"], [4, "val"], [3, "val"], [1, "val"]]

<%= link_to_remote "Click Here", 
        :url => {
        :controller => "controller", 
        :action => "method",
        :id => @p_id,
        :hash_list => hash_list
        },
        :method => 'post' %>

The link generated is :

[http://localhost/controller/method/12?hash_list%5B%5D%5B%5D=5&hash_list%5B%5D%5B%5D=val&hash_list%5B%5D%5B%5D=4&hash_list%5B%5D%5B%5D=val&hash_list%5B%5D%5B%5D=3&hash_list%5B%5D%5B%5D=val&hash_list%5B%5D%5B%5D=1&hash_list%5B%5D%5B%5D=val]

Could anyone tell me what is the right way to get something like: http://localhost/controller/method/12?hash_list=[hash_list]

so that i can use it as params[:agent_list] in my controller method.

P.S. sorry if its a nooby question.

2

There are 2 best solutions below

1
Mike Campbell On

Create a route in your routes.rb to the action, if not already created, then:

<%= link_to "Click here", my_route_path(@obj, :hash => { :foo => "bar" }), :remote => true, :method => :post %>

untested but should do the trick. If you're supplying remote: true then it doesn't really matter what your URL looks like, the hash should reside in params[:hash] in your controller.

link_to_remote is deprecated in Rails 3+.

Rails 2.3.8:

<%= link_to_remote "Click here", :url => my_route_path(@obj, :hash => { :foo => "bar" }), :method => :post %>
0
Denis On

My <%= link_to @item_link_name, show_item_path(item: {id: @item.id}) %>

generates http://localhost:3000/items/show_item?item%5Bid%5D=XXX,

what is recognised by controller as params[:item][:id]