【问题标题】:Get Path from array in Rails 4从Rails 4中的数组获取路径
【发布时间】:2015-08-24 19:06:57
【问题描述】:

我想知道是否有可能从数组外部视图(在我的情况下是模型)中获取路径,例如 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() 方法,但由于某些原因它必须是路径。

感谢您的帮助!

【问题讨论】:

  • 不幸的是,实际问题更复杂,因为我无法预测路径会是什么 --- 为什么?

标签: ruby-on-rails ruby-on-rails-4 routes


【解决方案1】:

我相信你需要同时包含这两个类

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

【讨论】:

  • 谢谢,就像我想要的那样:) 如果你能解释一下include ActionDispatch::Routing::PolymorphicRoutes 代表什么,我会很高兴?它似乎只适用于include Rails.application.routes.url_helpers
猜你喜欢
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 2021-08-14
相关资源
最近更新 更多