是什么导致这个未定义的方法`model_name'为Hash:Class

时间:2014-04-01 06:05:15

标签: ruby-on-rails ruby link-to

我正在尝试在rails中实现一个链接,该链接指向某个带有delete:方法的url,并且在url中有一些params,我正在尝试的是:

<%= link_to "Remove", [:remove_country, :admin, @species, my_param: country.id ], method: :delete %>

注意my_param:country.id我试图传入。这给了我结果:

undefined method `model_name' for Hash:Class

由于参数中的哈希值。不幸的是我需要那里,因为我试图将一个额外的参数传递给url(my_param,在这个例子中是country.id)

据我所知,这是将params传递到rails中的link_to的正确方法。我使用的正常link_to(没有my_param)在下面,完美地运行:

<%= link_to "Remove from this species", url_for([:remove_country, :admin, @species]), method: :delete %>

那么如何将我的参数合并到这个网址中呢?提前致谢。 (here is a source for why I think this should work

2 个答案:

答案 0 :(得分:1)

使用polymorphic_path([:remove_country, :admin, @species], {country_id: country.id})

答案 1 :(得分:0)

好的,我做了一个hacky解决方案,但我不喜欢它一点:

<%= link_to "Remove", url_for([:remove_country, :admin, @species]) + "?country_id=#{country.id}", method: :delete %>

非常难看,请有人让我摆脱痛苦,并向我展示一个很好的方法。

相关问题