0

I have a standard Ruby on Rails Spree Commerce application and managed to deploy it with Capistrano. Now I want to add an extra page to my that is displayed in the upper left menu as "About".

I don't really understand yet how Rails is working. A friend of mine told that Rails basically takes everything from git and that you have to make overrides or extra pages if you want to make something different.

So editing the "master" lay-out page (analogy with ASP.NET's MVC4) is not an option I guess for adding the menu item. Besides that I need a controller and the view.

The guide says that i should do the following:

Create a required controllers and a views directory:

mkdir -p app/controllers/spree
mkdir -p app/views/spree/about

Next, create a new file in the directory we just created called about_decorator.rb:

module Spree
  AboutController.class_eval do
    [Model parts I want to send to the view]

  end
end

We also need to add a route to this action in our config/routes.rb file. Let’s do this now. Update the routes file to contain the following:

Spree::Core::Engine.routes.draw do
  get "/about" => "about"
end

What I am wondering is how Rails works (is my friend's story about git and overrides true?) and regarding the site itself: how to make a link in the menu and how to tweak the page and add contents to it.

I hope someone can provides some insights, links, and/or explanations!

user2609980
  • 10,264
  • 15
  • 74
  • 143

1 Answers1

3

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.

Justin Wood
  • 9,941
  • 2
  • 33
  • 46
  • You are a hero! Thanks a lot. – user2609980 Jan 18 '14 at 15:07
  • Although you're answer helped a lot I still haven't managed to solve the issue. Can you take a look [here](http://stackoverflow.com/questions/21498208/how-to-add-new-view-to-ruby-on-rails-spree-commerce-app) and help me on the specifics? – user2609980 Feb 01 '14 at 12:47