【问题标题】:Using slugs in polymorphic_path在 polymorphic_path 中使用 slug
【发布时间】:2013-02-26 22:28:56
【问题描述】:

有没有办法可以在 polymorphic_path 中使用参数来传递 slug?

例如,我有以下路线

routes.rb

MyApp::Application.routes.draw do

  match "movies/:slug" => 'movies#show', :as=>:movie
  match "series/:slug" => 'series#show', :as=>:series

end

我有以下型号:

电影.rb

class Movie < ActiveRecord::Base
    has_many :cast_members, :as=>:media_item
end

Series.rb

class Series < ActiveRecord::Base
    has_many :cast_members, :as=>:media_item
end

CastMember.rb

class CastMember < ActiveRecord::Base
  belongs_to :media_item, :polymorphic=>true
end

这很好用,我可以从演员那里引用我的电影,反之亦然,就像正常的 has_many/belongs_to 关系一样。 我也可以在我的 cast_member 视图中执行此操作:

*cast_members/show.html.erb*

link_to (@cast_member.movie.title, movie_path(@cast_member.movie.slug))

返回“电影/电影标题”

我能做到

*cast_members/show.html.erb*

link_to (@cast_member.movie.title, polymorphic_path(@cast_member.media_item))

但这会返回“/movies/24”

我尝试过以不同的方式将 slug 作为项目传递给 polymorphic_path,例如

link_to (@cast_member.movie.title, polymorphic_path(@cast_member.media_item, @cast_member.media_item.slug))
link_to (@cast_member.movie.title, polymorphic_path(@cast_member.media_item, :slug=>@cast_member.media_item.slug))
link_to ([@cast_member.movie.title, polymorphic_path(@cast_member.media_item, @cast_member.media_item.slug]))

但这些都返回错误或带有 id 的路径。

如何让 polymorphic_path 使用 movie.slug 而不是 id?

【问题讨论】:

  • 你是如何产生这些蛞蝓的?你在使用friendly_id (github.com/norman/friendly_id)吗?
  • 我正在使用 stringex 并在标题上调用 .to_url。我一直在手动完成,没有库,只定义我自己的 .to_slug 方法,但它缺少像ñ这样的东西,所以我切换到 stringex。

标签: ruby-on-rails polymorphic-associations


【解决方案1】:

我改用friendly_id 来生成slug。它神奇地在后台神奇地处理所有 slugid 转换,并解决了问题。

我确实认为 Rails 应该有一种内置的方式来执行此操作,就像您可以将 slug 传递给默认的 *_path 方法一样。

【讨论】:

    【解决方案2】:

    我通过使用 Rails 的内置路径助手而不是 polymorphic_path 解决了这个问题。我真的很想用那个方法,因为它需要使用模型的 ID,我不能。

    在我的应用程序中,我有很多“slugable”模型,因此在 slugable mixin 中包含 #to_path 方法是有意义的。

    # app/models/concerns/slugable.rb
    module Slugable
      extend ActiveSupport::Concern
    
      included do
        validates :slug, presence: true, uniqueness: {case_sensitive: false}
      end
    
      def to_path
        path_method = "#{ActiveModel::Naming.singular_route_key(self)}_path"
        Rails.application.routes.url_helpers.send(path_method, slug)
      end
    
      def slug=(value)
        self[:slug] = value.blank? ? nil : value.parameterize
      end
    end
    

    然后在模板中:

    <%= link_to my_slugable_model.name, my_slugable_model.to_path %>
    

    如果您的路由中有嵌套资源,则需要调整该资源的代码。

    类似这样的东西(未经测试):

    def to path(my_has_many_model_instance)
       class_path = self.class.to_s.underscore
       has_many_class_path = my_has_many_model_instance.class.to_s.underscore
       path_method = "#{self_path}_#{has_many_class_path}_path"
       Rails.application.routes.url_helpers.send(path_method, slug, my_has_many_model)
    end
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      相关资源
      最近更新 更多