How to Add New View to Ruby on Rails Spree Commerce App

How to add new view to Ruby on Rails Spree commerce app?

You need to do in routes.rb:

Spree::Core::Engine.routes.prepend do
get '/about', :to => 'about#index', :as => :about
end

or without the Spree::Core scope:

get '/about', :to => 'spree/about#index', :as => :about

Because, you have your about_controller.rb i.e. AboutController defined inside Spree module. And, hence you'll have to reference the spree namespace in your route to set it properly.

In your views:

<li id="about-link" data-hook><%= link_to Spree.t(:about), spree.about_path %></li>

or

<li id="about-link" data-hook><%= link_to Spree.t(:about), main_app.about_path %></li>

How to add an extra menu item plus view to Rails Spree Commerce application

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.

Adding route to Ruby on Rails Spree Commerce app?

I had this same problem on a project where it wasn't cost effective to dig around. I fixed it by adding a redirect:

match "/store/user/spree_user/logout", :to => redirect('/store/logout')

Where 'store' is your Spree root.

How to customize spree views completely?

Read this guides. Everything is explained very clearly.

Can I import spree views to my application rails views

Yes, as in any other gem. It is the easiest way and explained in "Template Replacements" section of guide.

Adding Routes to Rails' Spree E-Commerce

I encountered the same error and found this solution, which solved it by prefixing main_app, before each of my_paths/_urls. In my case, these were links used in one of the /override.rb files.

So, try: main_app.about_us_path.

Customise registration page in spree commerce 2.1

Either use Deface to slightly modify that view (make new file with 'same' path (app/overrides/spree/checkout/registration.rb) so you will have them organized) or create new view in your app/views/spree/checkout/ directory named registration.html.erb, it should be rendered instead of default one.

Edit: I misunderstood you, to turn off unwanted override you should add another override with path specified above and put into it:

Deface::Override.new(
virtual_path: 'spree/checkout/registration',
name: "name_of_override", # you have to provide proper name here
disabled: true)

To find out right name you can look into every spree extension for overrides of registration.rb or into your logs, all applied overrides should be listed there after visiting that page.

UPDATE BY ORIGINAL POSTER:

It turns out it's an override in the spree_auth_devise gem: https://github.com/spree/spree_auth_devise/blob/master/app/overrides/auth_user_login_form.rb



Related Topics



Leave a reply



Submit