mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Define The Root Path For The App as a Rails TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1456 TILs and counting..._
|
_1457 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -899,6 +899,7 @@ _1456 TILs and counting..._
|
|||||||
- [Custom Validation Message](rails/custom-validation-message.md)
|
- [Custom Validation Message](rails/custom-validation-message.md)
|
||||||
- [Customize Paths And Helpers For Devise Routes](rails/customize-paths-and-helpers-for-devise-routes.md)
|
- [Customize Paths And Helpers For Devise Routes](rails/customize-paths-and-helpers-for-devise-routes.md)
|
||||||
- [Customize The Path Of A Resource Route](rails/customize-the-path-of-a-resource-route.md)
|
- [Customize The Path Of A Resource Route](rails/customize-the-path-of-a-resource-route.md)
|
||||||
|
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
|
||||||
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
||||||
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
|
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
|
||||||
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
|
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
|
||||||
|
|||||||
37
rails/define-the-root-path-for-the-app.md
Normal file
37
rails/define-the-root-path-for-the-app.md
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# Define The Root Path For The App
|
||||||
|
|
||||||
|
The `root_path` helper that you might want to use in Rails controllers and
|
||||||
|
views is not available by default.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> Rails.application.routes.url_helpers.root_path
|
||||||
|
|
||||||
|
ruby/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.14.0/lib/irb.rb:1285:in `full_message': undefined method `root_path' for #<Module:0x0000000106d11738> (NoMethodError)
|
||||||
|
|
||||||
|
Rails.application.routes.url_helpers.root_path
|
||||||
|
^^^^^^^^^^
|
||||||
|
Did you mean? logout_path
|
||||||
|
book_path
|
||||||
|
```
|
||||||
|
|
||||||
|
It needs to be declared in the `config/routes.rb` file with the controller
|
||||||
|
action that it points to.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# config/routes.rb
|
||||||
|
Rails.application.routes.draw do
|
||||||
|
root 'home#index'
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Once this is defined the `root_path` will now be available with the rest of
|
||||||
|
your URL helpers.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
better-reads(dev)> reload!
|
||||||
|
Reloading...
|
||||||
|
better-reads(dev)> Rails.application.routes.url_helpers.root_path
|
||||||
|
=> "/"
|
||||||
|
```
|
||||||
|
|
||||||
|
[source](https://guides.rubyonrails.org/routing.html#using-root)
|
||||||
Reference in New Issue
Block a user