繁体   English   中英

Ruby on Rails-使用link_to_remote Rails 2将哈希作为参数传递

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

我正在使用Rails 2.3.8版中的哈希值=> 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' %>

生成的链接是:

[HTTP://本地主机/控制器/方法/ 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]

谁能告诉我什么是正确的方法来获得类似的东西:http:// localhost / controller / method / 12?hash_list = [hash_list]

这样我就可以在控制器方法中将其用作params [:agent_list]。

PS抱歉,如果它是一个nooby问题。

routes.rb创建到操作的路由(如果尚未创建),则:

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

未经测试,但应该可以解决问题。 如果您要提供remote: true那么URL的外观并不重要,哈希应该驻留在控制器的params[:hash]中。

在Rails 3+中已弃用link_to_remote

Rails 2.3.8:

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

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

生成http://localhost:3000/items/show_item?item%5Bid%5D=XXX

控制器识别为params[:item][:id]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM