【问题标题】:Rails 3 Overriding Named RoutesRails 3 覆盖命名路由
【发布时间】:2010-11-26 15:29:13
【问题描述】:

对于 Commentable 实现,我有一个类似多态的关联(不是真正的 Rails 关联)。不过,我希望能够对所有 cmets 使用相同的视图。对于我命名的路由,我只想能够调用edit_comment_path 并让它转到我的新方法。

我的路线将如下所示:

resources :posts do
  resources :comments
end

resources :pictures do
  resources :comments
end

resources :comments

现在我已经在一个辅助模块中覆盖了edit_comment_path,但是resources :comments 生成的那个却一直被调用。我保留了resources :comments,因为我希望能够直接访问 cmets 和一些我依赖的 Mixins。

这是我在module CommentsHelper 中的覆盖方法:

  def edit_comment_path(klass = nil)
    klass = @commentable if klass.nil?
    if klass.nil?
      super
    else
      _method = "edit_#{build_named_route_path(klass)}_comment_path".to_sym
      send _method
    end

编辑

# take something like [:main_site, @commentable, @whatever] and convert it to "main_site_coupon_whatever"
  def build_named_route_path(args)
    args = [args] if not args.is_a?(Array)
    path = []
    args.each do |arg|
      if arg.is_a?(Symbol)
        path << arg.to_s 
      else
        path << arg.class.name.underscore
      end
    end
    path.join("_")
  end

【问题讨论】:

    标签: ruby-on-rails routing


    【解决方案1】:

    实际上,这些都不是必需的,内置的 polymorphic_url 方法工作得很好:

    @commentable 在 CommentsController 的 before_filter 中设置

    <%= link_to 'New', new_polymorphic_path([@commentable, Comment.new]) %>
    
    <%= link_to 'Edit', edit_polymorphic_url([@commentable, @comment]) %>
    
    <%= link_to 'Show', polymorphic_path([@commentable, @comment]) %>
    
    <%= link_to 'Back', polymorphic_url([@commentable, 'comments']) %>
    

    编辑

    class Comment < ActiveRecord::Base
       belongs_to :user
       belongs_to :commentable, :polymorphic => true
    
       validates :body, :presence => true 
    end
    
    
    
    class CommentsController < BaseController
    
      before_filter :find_commentable
    
    private 
    
      def find_commentable
        params.each do |name, value|
          if name =~ /(.+)_id$/
            @commentable = $1.classify.constantize.find(value)
            return @commentable
          end
        end
        nil
      end
    
    end
    

    【讨论】:

    • 嘿,谢谢。我也正要进入那个元漩涡。我在 Rails 文档中找不到太多关于此的内容——似乎在博客等中很友好。您有机会分享您的@commentable 逻辑吗?这很有趣
    • @Joseph Weissman 看到我的更新。像这样的东西应该工作。我从旧代码中剪切并粘贴了它,因为问题有点老了。
    【解决方案2】:

    您可能在尝试使用辅助模块覆盖路线时遇到问题。尝试在您的 ApplicationController 中定义它,并将其设为helper_method。您还可以调整名称以避免冲突并将其用作包装器,如下所示:

    helper_method :polymorphic_edit_comment_path
    def polymorphic_edit_comment_path(object = nil)
      object ||= @commentable
      if (object)
        send(:"edit_#{object.to_param.underscore}_comment_path")
      else
        edit_comment_path
      end
    end
    

    【讨论】:

    • 我认为包装方法是最好的,因为它使事情更具可读性。快速提问。我有自己的方法 build_named_route_path(klass) 可以采用像 [:main, @commentable, @object] 这样的数组。有没有更简单的方法来使用 ActiveSupport 解析这个?
    • 另外,您发布的方法不起作用。我的适用于除索引视图之外的所有视图。很奇怪。
    猜你喜欢
    • 2011-08-25
    • 2011-07-19
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多