What is causing this undefined method `model_name' for Hash:Class

独自空忆成欢 提交于 2019-12-11 14:32:40

问题


I am trying to implement a link in rails that will point to a certain url with a delete: method and with some params in the url, what I am trying is this:

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

Note the my_param: country.id I am trying to pass in. This gives me the result:

undefined method `model_name' for Hash:Class

due to the hash in the params. Unfortunately I need that there as I am trying to pass an extra param into the url (my_param which is country.id in this example)

as far as I can remember this is the correct method to pass params into a link_to in rails. The normal link_to that I use (without the my_param) is below, that works perfectly:

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

So how do I incorporate my param into this url? thanks in advance. (here is a source for why I think this should work)


回答1:


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




回答2:


Ok, I made a hacky solution but I don't like it one bit:

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

It's very ugly, please someone put me out of my misery and show me a nice way to do this.



来源:https://stackoverflow.com/questions/22778027/what-is-causing-this-undefined-method-model-name-for-hashclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!