从URL中提取按名称参数加载供应商对象 [英] Load Vendor Object By Name Parameter Extracted From The URL

查看:97
本文介绍了从URL中提取按名称参数加载供应商对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一条路由,该路由链接到每个 Spree Commerce .该页面应列出详细信息(名称,关于我们的信息等)以及给定供应商的产品.

I'm trying to create a route that link to the store page of each Vendor using Spree Commerce. The page should list the details (name, about us, etc), and the products of the given vendor.

https://example.com/stores/vendor-one

我的 routes.rb 配置:

My routes.rb configuration :

Spree::Core::Engine.routes.draw do
    resources :stores
end

我的 stores_controller 控制器:

My stores_controller controller :

module Spree
    class StoresController < Spree::StoreController  
      require 'vendor'
      def show
        @vendor = current_spree_vendor
      end     
    end
end

我的 show.html.erb 模板:

My show.html.erb template :

<h2><%= @vendor.name %></h2>
<p><%= @vendor.about_us %></p>

我得到的错误:

Spree :: Stores#single中的NoMethodError

NoMethodError in Spree::Stores#single

在第2行出现的位置显示/myapp/app/views/spree/stores/show.html.erb:

Showing /myapp/app/views/spree/stores/show.html.erb where line #2 raised:

nil:NilClass的未定义方法名称"

undefined method `name' for nil:NilClass

第2行:<h2><%= @vendor.name %></h2>

如何加载给定的 <通过网址获得strong>供应商 的名称?我是Rails的新手,所以我们将不胜感激!

How can I load the given Vendor by getting its name from the url ? I'm new to Rails so any help will be highly appreciated !

推荐答案

像这样调试错误的方法是从错误中倒退.因为@vendornil,所以模板中发生错误.为什么?控制器具有#show方法,该方法设置@vendor,但是将@vendor设置为current_spree_vendor的结果,因此必须是current_spree_vendor返回的nil.因此,现在您需要检查current_spree_vendor并弄清为什么有时返回nil的原因.

The way to debug errors like this is by working backwards from the error. The error is occurring in the template because @vendor is nil. Why? The controller has a #show method that is setting @vendor, but it is setting @vendor to the result of current_spree_vendor, so it must be that current_spree_vendor returned nil. So now you need to examine current_spree_vendor and figure out why it is sometimes returning nil.

您很快将学习如何调试此类错误,并且由于这些错误太常见,太具体而无法成为Internet可以帮助您的东西,因此您将需要进行调试.即使Internet可以提供帮助,它也太慢了,等待答案可以阻止您完成工作.

You will soon learn how to debug this sort of error, and you'll need to because these kinds of errors are too common, and too specific, to be something that the internet can help you with. Even when the internet can help, it's too slow and waiting for answers on it will keep you from getting the job done.

这篇关于从URL中提取按名称参数加载供应商对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆