DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

path resolves

πŸ‘ Case: in not Controller

Assume that, you're in app/service.

class SomeService
  # πŸ¦„ include the url_helpers
  include Rails.application.routes.url_helpers

  user_path
end

πŸ‘ Case: in Rails console

app.user_path
#=> "/user"

πŸ‘ Case: check current path & action name

[1] pry(#<UserController>)> controller_path
=> "user"

[2] pry(#<UserController>)> action_name
=> "index"

πŸ‘ Case: Get an absolute path from the current source file

There are two ways.

File.expand_path(relative_path, __FILE__) # from the current source file
File.expand_path(relative_path, __dir__)  # from the current source file

πŸ”— Parent Note

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: