Rails:更改通过 link_to 在 ActiveRecord::Base 对象上调用的默认方法

发布于 2024-12-26 06:16:43 字数 698 浏览 5 评论 0原文

我有一个 gem,它本质上只是 gems 命名空间中的一堆 ActiveRecord::Base 类。对于他们所有人,我都遇到了同样类型的问题。我有模型...

module MyGem
  class User < ActiveRecord::Base
    ...
  end
end

然后在我的应用程序中我有路线...

resources :users

搞砸的是 link_to...

<% @users.each do |user| %>
  <td><%= link_to 'Show', user %></td>
<% end %>

我明白...

undefined method `my_gem_user_path' for #<#<Class:0x0000000305f728>:0x00000003055408>

我在routes.rb 中尝试了各种东西,但我在想解决方案可能是将 model/link_to 配置为默认调用 user_path(user) 而不是 my_gem_user_path(user) 。我只是不知道如何(如果可能的话)做到这一点。

有人知道这里的最佳实践吗?

I have a gem which is essentially just a bunch of ActiveRecord::Base classes in the gems namespace. For all of them I get the same type of problem. I have the model...

module MyGem
  class User < ActiveRecord::Base
    ...
  end
end

Then in my app I have the routes...

resources :users

What gets screwed up is in the link_to...

<% @users.each do |user| %>
  <td><%= link_to 'Show', user %></td>
<% end %>

I get...

undefined method `my_gem_user_path' for #<#<Class:0x0000000305f728>:0x00000003055408>

I've tried various things in routes.rb, but I'm thinking the solution may be to configure the model/link_to to call user_path(user) instead of my_gem_user_path(user) by default. I just don't know how, if at all possible, to do this.

Anybody know the best practice here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金兰素衣 2025-01-02 06:16:43

我认为这里的最佳实践是为您的路线命名。您的类的命名空间为 MyGem::User。 #link_to 使用 #polymorphic_path 查询类的类名,然后对该类名调用 #underscorize 来获取路径,因此它假设路径为 my_gem_user_path,而不是 user_path。这意味着资源调用应该像这样包装:

namespace(:my_gem) do
  resources :users
end

然后,my_gem_user_path 将解析为 MyGem::UsersController。确保您的控制器也具有这样的命名空间。如果您不希望路由解析到该控制器,则可以将控制器名称作为#resources 的选项提供。

编辑:这很有趣,我认为不会遇到这个问题。可能仍然有一些配置可以解决这个问题,所以我不会将其标记为已解决。您始终可以用命名路由替换 polymorphic_path (当您使用上面的面向资源的路径时调用的路径)。例如,代替
link_to“用户”,用户
你总是可以明确地使用
link_to“用户”,my_gem_user_path
当您在routes.rb 文件中使用RESTful 路由(使用#resources 方法)时,它会为您提供这些命名路由。

对于form_for,语法略有不同:

form_for(@user, @task)

becomes

form_for([@user, @task], :url => my_gem_user_my_gem_tasks_path(@user, @task), :method => :post)

for a new form。对于编辑表单,这将是

form_for(@task, :url => my_gem_user_my_gem_tasks_path(@user, @task), :method => :put)

这有意义吗?这是 form_for 文档的链接 http://api .rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

I think the best practice here is to namespace your routes. Your class is namespaced as MyGem::User. #link_to uses #polymorphic_path to query the class for its class name and then calls #underscorize on that class name to get the path, so it assumes that the path will be my_gem_user_path, not user_path. That means the resources call should be wrapped like so:

namespace(:my_gem) do
  resources :users
end

Then, my_gem_user_path will resolve to the MyGem::UsersController. Make sure your controller is namespaced like that as well. If you don't want the route to resolve to that controller, you can provide the controller name as an option to #resources.

edit: That's interesting, I didn't think it would run into that problem. There may still be some configuration to fix that, so I wouldn't mark this as solved yet. You can always replace the polymorphic_path (which is what is getting called when you use the resource-oriented path like above) with named routes. For example, instead of
link_to "User", user
you can always explicitly use
link_to "User", my_gem_user_path
When you use RESTful routes in your routes.rb file (with the #resources method), it provides you with those named routes.

For the form_for, the syntax is slightly different:

form_for(@user, @task)

becomes

form_for([@user, @task], :url => my_gem_user_my_gem_tasks_path(@user, @task), :method => :post)

for a new form. For an edit form, it would be

form_for(@task, :url => my_gem_user_my_gem_tasks_path(@user, @task), :method => :put)

Does that make sense? Here's a link to the documentation for form_for http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文