简体   繁体   中英

Rails Model/Controller Structure

I've recently just gotten into programming with ruby on rails, and I've been having trouble grasping how exactly I should be structuring and organizing all my code, specifically exactly how to differentiate what certain scaffolds/models/controllers should be doing.

For example, I have an app in which I want to have a front page with just an introduction page, giving the viewer an option to login or register (standard welcome screen). I've generated a Users scaffold, and I was planning on using this to handle all the main pages that the User sees in terms of their profile. I've also been using devise to handle login and registration.

However, I'm unsure exactly how to handle this welcome page. As of now, I've merely altered the index.html.erb that the Users scaffold had automatically generated, but it doesn't really seem to fit, since it's not really a page that has to do with the User profile.

Is there a general convention on how to separate the functionality of controllers? What should I be doing with pages that don't necessarily have anything to do with specific database aspects (for example, contact pages, welcome screens, etc.).

Also, I'm a bit confused as to what the default application_controller.rb's purpose is. It looks like everything is extending it, but should I be altering this (possibly to use for the generic pages I mentioned above)?

Thanks in advance, sorry for the extremely long post!

If you want a generic homepage, just generate a controller by whatever name you please. I have two recent ones named play and lycee.

You're right, ApplicationController is mostly to be extended. Sometimes you will put some sitewide code in there, but it will be small.

Your index or home page will probably be a single controller with a view attached. You can generate the controller by typing:

rails generator controller Home main

Note that you can shorten this to

rails g controller Home main

Afterwards if you edit your routes.rb file you can attach this controller and action to the root directive:

root :to => 'home#main'

The ApplicationController is what all your other controllers will extend from. If you want to share code between your controllers then you should put that code in the ApplicationController .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM