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

流过昼夜 提交于 2019-11-29 14:48:11

问题


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

回答1:


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>


来源:https://stackoverflow.com/questions/21498208/how-to-add-new-view-to-ruby-on-rails-spree-commerce-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!