How can I add a new view to my Ruby on Rails application?

Where is the best place to add a new view to a Rails app? I have three main controllers and then the Application controller. I folders in views for the three controllers. I want to add a homepage view to my application, separated from the other views. Where would be the best place to add it? Should I make a new folder in views and add a homepage there? Can I add it as part of the application controller?

1 answer

  • answered 2021-11-28 22:31 JohnP

    If you want to have Rails work consistently and easily, it's best to follow the Rails conventions. So, the views for a particular controller would go into the app/views directory, in a subdirectory named for the particular controller. So, the view for your pages_controller would go into app/views/pages - and you'd have a view named for each relevant action in your controller (show.html.erb, for example).

    Generally, Rails operates by "convention over configuration". So, if you follow the conventions, you'll find everything works nice and smooth. If you try to create your own conventions, at some point it will bite you and things will stop working.

    So, the basic question is: which controller will render your new home-page view? I'm guessing probably static_pages_controller or similar. In which case, the view would go into app/views/static_pages.

    If you don't have a controller then Rails won't render the view at all.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum