【问题标题】:How to add new view to Ruby on Rails Spree commerce app?如何向 Ruby on Rails Spree 商务应用程序添加新视图?
【发布时间】:2014-02-01 12:43:52
【问题描述】:

我似乎无法解决的一个非常基本的问题是如何向我的 Ruby on Rails Spree 商务应用程序添加新视图。我想要做的是在 _main_nav_bar.html.erb 中的主页链接旁边有一个链接,当您单击它时,会在显示产品的位置显示一个关于页面。所以:

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

点击关于导致:

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

在 views/shared/_main_nav_bar.html.erb 中,我创建的链接(基于主页链接)如下所示:

<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>

我创建的 AboutController 如下所示:

module Spree
  class AboutController < Spree::StoreController

    def index

    end
  end
end

最后,我在 config/routes.rb 中添加了以下代码:

root :about => 'about#index'

当我现在尝试启动服务器时,它不再工作而没有给出错误消息。

有人可以帮我解决这个问题吗?如何添加视图并创建加载到主 div 中的工作链接?

额外: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

【问题讨论】:

  • 在你的 routes.rb 中尝试:root :about =&gt; 'spree/about#index'
  • 感谢您的帮助。我做到了,它说:ArgumentError missing :controller Extracted source (around line #63): root :about =&gt; 'spree/about#index' 我确实在 spree/about_controller.rb 中有控制器,我把它放在了帖子中。
  • 应该是root :to =&gt; 'spree/about#index'。对不起。
  • 现在 routes.rb 中的代码看起来像这样:root :to =&gt; 'home#index' root :about =&gt; 'spree/about#index' 它给出了错误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.
  • 老兄,您不能在一个应用程序中拥有多个 root。要么这样做:get '/about' =&gt; 'spree/about#index' 你的情况。

标签: ruby-on-rails ruby spree


【解决方案1】:

你需要在routes.rb中做:

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

或没有Spree::Core 范围:

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

因为,您的 about_controller.rb 即在 Spree 模块中定义了 AboutController。而且,因此您必须在路由中引用 spree 命名空间才能正确设置它。

在你看来:

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

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

【讨论】:

  • 到目前为止,您帮了很多忙,但还没有完全奏效。我仍然收到错误:NameError in Spree::Home#index undefined local variable or method about_path in &lt;li id="about-link" data-hook&gt;&lt;%= link_to Spree.t(:about), about_path %&gt;&lt;/li&gt;
  • 如果它没有解决您的问题,那么您为什么接受这个答案?好吧.. idk 为什么about 不起作用,因为:as =&gt; :about 应该为您的应用程序设置两个路由助手:about_pathabout_url。而且,我发现您在 about_path 处遇到错误很奇怪..
  • 到目前为止,你帮了我这么多,我以为我给了你一些功劳。 ;-)
  • 看到这个:guides.rubyonrails.org/routing.html#naming-routes 你可以找到,对于注销,你得到 logout_path 和 logout_url。所以,我不知道为什么它在这里不起作用。查看更新,也许它有帮助。 ://
  • 使用 main_app 为路由添加前缀有效。如&lt;li id="about-link" data-hook&gt;&lt;%= link_to Spree.t(:about), main_app.about_path %&gt;&lt;/li&gt;!所以现在整个问题都解决了!也许您可以使用其中的 main_app 前缀更新您的答案,并解释“因为您在 spree 模板中调用它,所以需要在它前面加上 main_app.,例如 main_app.products_starting_with_path”。 :-) 非常感谢您的努力!
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多