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

1

6

A very basic question that I cannot seem to solve is how to add a new view to my Ruby on Rails Spree commerce application. What I want to do is have a link next to the Home link in the _main_nav_bar.html.erb and when you click it have displayed an about page at the place where the products are displayed. So:

home about       cart
---------------------
things of the HOME page
---------------------
footer

Click on about leads to:

home about       cart
---------------------
things of the ABOUT page
---------------------
footer      

In views/shared/_main_nav_bar.html.erb the link I created (based on the home link) looks as follows:

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

The AboutController I created looks as follows:

module Spree
  class AboutController < Spree::StoreController

    def index

    end
  end
end

And finally, in config/routes.rb I added the following code:

root :about => 'about#index'

When I now try to start the server it just does not work anymore without giving an error message.

Can someone please help me out on this issue? How do I add a view and create a working link that loads in the main div?

EXTRA: routes.rb

MyStore::Application.routes.draw do


  mount Spree::Core::Engine, :at => '/'

  Spree::Core::Engine.routes.prepend do
     #get 'user/spree_user/logout', :to => "spree/user_sessions#destroy"
  end


  get '/about' => 'spree/about#index'
  get '/contact' => 'spree/contact#index'


end
Majormajordomo answered 1/2, 2014 at 12:43 Comment(16)
try: root :about => 'spree/about#index' in your routes.rbGreenhorn
Thanks for your help. I did and it says: ArgumentError missing :controller Extracted source (around line #63): root :about => 'spree/about#index' I do have the controller in spree/about_controller.rb which I placed in the post.Majormajordomo
it should be root :to => 'spree/about#index'. Sorry.Greenhorn
Now the code in routes.rb looks like this: root :to => 'home#index' root :about => 'spree/about#index' and it gives the error ArgumentError Invalid route name, already in use: 'root' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming.Majormajordomo
Dude, you can't have multiple root in one application. Either do this: get '/about' => 'spree/about#index' for your case.Greenhorn
Okay seems logical, when I do get it says it cannot find the controller again. Message: Argumenterror missing :controller Extracted source (around line #63): root :to => 'home#index' get :about => 'spree/about#index'.Majormajordomo
Did you try what I wrote? Does it work?Greenhorn
I updated the routes.rb with the get '/about' => 'spree/about#index' and the server starts again without errors! Thanks! The link does not work though. I have <li id="about-link" data-hook><%= link_to Spree.t(:about), spree.root_path%></li>, so naturally it links to spree.root_path still? How do I make this link go to /about?Majormajordomo
You're new to spree, I suppose? Do: get '/about' => 'spree/about#index', :as => :about in routes.rb and in link(view template): <li id="about-link" data-hook><%= link_to Spree.t(:about), about_path%></li>. this should work.Greenhorn
Yes quite new! :) For now: about_path is undefined. Where do I define these variables?Majormajordomo
I am sorry, I am naive. Pardon me. Did you do: get '/about' => 'spree/about#index', :as => :about in your routes.rb?Greenhorn
Yes. And now the localhost:3000/about links correctly to the about.html.erb page I made in views/about/index.html.erb. The only issue is how to make the link in the _main_nav_bar.html.erb. Then we are there ;-).Majormajordomo
link: <li id="about-link" data-hook><%= link_to Spree.t(:about), about_path%></li> should work. otherwise try with: spree.about_path instead of about_pathGreenhorn
Still get the undefined method about_path for #<ActionDispatch::Routing::RoutesProxy:0xc8008d4> when I do spree.about_path or only about_path. Do I need to map this about_path to /about somewhere?Majormajordomo
Can you post routes.rb file? I am guessing you have get '/about' .. with in a block or may be helper isn't being set properly. if not then Do: rake routes|grep about in terminal(by cd to your app's directory) and post the output.Greenhorn
Adder routes.rb to question and output of rake routes|grep about is about GET /about(.:format) spree/about#indexMajormajordomo
G
5

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>
Greenhorn answered 1/2, 2014 at 13:48 Comment(10)
Well you helped a lot so far, but it is not completely working. I still get the error: NameError in Spree::Home#index undefined local variable or method about_path in <li id="about-link" data-hook><%= link_to Spree.t(:about), about_path %></li>Majormajordomo
If it didn't solve your problem then why you accepted the answer? Well.. idk why about isn't working because, :as => :about should set two routing helpers:about_path and about_url for your application. And, I find it weird that you're getting error at about_path..Greenhorn
Ha you helped me so much so far I thought I give you some credit. ;-)Majormajordomo
See this: guides.rubyonrails.org/routing.html#naming-routes as you can find, for logout, you get logout_path and logout_url. So, I am clueless why it isn't working here. see the update, maybe it helps. :/Greenhorn
it is an issue with Spree: see #10065033.Majormajordomo
Oh Okay. So, one work around can be by wrapping it within the spree block. See the update.Greenhorn
If I use your updated routes.rb code it does work in the sense that the server starts, and that the link is displayed when I hover. But when I click it I get the error: uninitialized constant Spree::Spree with a list of the routes underneath. Any ideas?Majormajordomo
please copy and paste the exact code I wrote. you must have had: get '/about', :to => 'spree/about#index', :as => :about code inside the Spree::Core block. :(Greenhorn
I have the correct code. In routes.rb there is the main MyStore:Application.routes.draw block and in that block a Spree::Core::Engine.routes.prepend block. I am able to visit the site if I place it in the latter block and refresh a few times. But then I get the error when I actually click the link.Majormajordomo
Prefixing the route with main_app worked. As in <li id="about-link" data-hook><%= link_to Spree.t(:about), main_app.about_path %></li>! So now the whole problem is solved! Maybe you can update your answer with this main_app prefix in it and the explanation that "Because you're calling this inside a spree template you need to prefix it with main_app., like main_app.products_starting_with_path". :-) And thanks a lot for your effort!Majormajordomo

© 2022 - 2024 — McMap. All rights reserved.