How Rails works
This is going to be a simple explanation of MVC, and the basic internals of the framework.
MVC
Model - A model is only the data of your application. There is not a database required, though in the rails world, a model usually has some database connected.
View - A view is strictly what the user sees. There should be very little logic in views. Only what would be considered "view logic". This being a simple check to see if a user is logged in, so you should display either their name or the login button. Though, some people would argue to put that into a view helper. Either way, you should not be doing calculations in your views. That is the point of the model.
Controller - A controller is meant to be a very thin layer that just grabs the required information for a request. A request in this case is a single page view.
There is a common thought process among rails developers, and that is "thin controller, fat model". This basically states that your controllers should be very small. Most of your applications logic should be contained in the model, leaving only requesting information in your controllers.
NOTE: Your models should have no knowledge of your views, and your views should not have any knowledge of your models. Meaning, your view should never use your model directly.
Basic Internals
config/routes.rb
- Is a file that describes all of the routes in your application. A route is something like http://example.com/users/1
, which would most likely point to a user profile. Routes are used to let the application know which controllers, methods, and names to use for each request.
rake routes
- rake
is a command line tool that is quite common among ruby applications. It lets you create tasks, and execute them. In this case, the routes
task parses the entire config/routes.rb
file and tells you what routes are available in your application, and the corresponding names given to each route.
partial views - A partial view is, simply put, only a part of a view. This is a common thing to do, especially for forms that can be used on multiple pages of a single application. The reason for using partials is usually to remove duplication through your views. All partial views are named with an _
to prefix. So, it could be something like _navigation.html.erb
or _form.html.haml
. (erb and haml are two common HTML rendering engines).
Answering your questions
Adding an Item to the Navigation Bar
Adding an item to the navigation bar is very similar to editing the "master page" in an ASP application. You just need to find the partial that needs to be edited. in your views, follow the <%= render :partial => 'spree/shared/header' %>
declarations until your find the file you need to edit. Note, I am not positive if that is the actual file you need to follow, check out the spree_application.html.erb
file in order to see which one you need to follow. That file is, in essence, the same as the "master pages" you are used to using.
Being Able to Actually Use the New Link
You will need to create a new controller, or if there is already one that suites the task you are attempting to accomplish, you can just create a new method in that controller.
If you need to manipulate or display some data, you will also need a model. You can either create one, or just use any that is responsible for the data you already have.
You will need to add a route in the routes.rb
file to allow your application to know which controller#method
to use.
Running rake routes
will give you the information about the new route you just added. Note, if you have a route named user_profile
, you are given convenience methods to allow you to make a link to the route easily. You can use either user_profile_path
or user_profile_url
in order to make a link. I would suggest using *_path
to make links on your own site, as it uses a relative path, /user/1
instead of http://example.com/user/1
. *_url
will give the latter. Generally only use *_url
if you are making some form of API and want to make a link back to your application.