res
res

Reputation: 1225

Get Path from array in Rails 4

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!

Upvotes: 3

Views: 511

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

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

Upvotes: 3

Related Questions