Get Path from array in Rails 4

507 Views Asked by At

I wonder is there possibility to get path from array outside views (in my case in model) like in link_to method - link_to 'User', [:admin, @user] .

For example I want to store admin_artist_path (unfortunately real problem is more complex because I can't predict what path will be )

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. I know about url_for() method but for some reasons it must be path.

Thanks for help!

1

There are 1 best solutions below

1
On BEST ANSWER

I believe you need to include both of these classes

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