【问题标题】:What is causing this undefined method `model_name' for Hash:Class是什么导致了 Hash:Class 的未定义方法“model_name”
【发布时间】:2014-04-01 06:05:14
【问题描述】:

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

<%= 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)

据我所知,这是将参数传递到 rails 中的 link_to 的正确方法。我使用的正常 link_to(没有 my_param)如下,效果很好:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      这很丑陋,请有人把我从痛苦中解救出来,并告诉我一个很好的方法。

      【讨论】:

      猜你喜欢
      • 2013-05-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多