DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

2

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
Enter fullscreen mode Exit fullscreen mode

👍 Case: in Rails console

app.user_path
#=> "/user"
Enter fullscreen mode Exit fullscreen mode

👍 Case: check current path & action name

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

[2] pry(#<UserController>)> action_name
=> "index"
Enter fullscreen mode Exit fullscreen mode

👍 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
Enter fullscreen mode Exit fullscreen mode

🔗 Parent Note

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay