从 Rails 4 中的数组获取路径

Get Path from array in Rails 4

我想知道是否有可能像 link_to 方法 - link_to 'User', [:admin, @user] .

那样从数组外部视图(在我的模型中)获取路径

例如我想存储admin_artist_path(不幸的是实际问题更复杂,因为我无法预测路径是什么)

class MenuItem < ActiveRecord::Base

  include Rails.application.routes.url_helpers
  before_save :store_path!

  private

    def store_path!
      self.url = [:namespace, :artist].do_some_magick
    end

  end

ps。我知道 url_for() 方法,但由于某些原因它必须是路径。

感谢帮助!

我认为您需要包括这两个 类

include ActionDispatch::Routing::PolymorphicRoutes
include Rails.application.routes.url_helpers

before_save :store_path!

private

def store_path!
  self.url = polymorphic_path([:namespace, :artist])
end