我正在运行一个rails 2.3.5 app并获得一个nil:Nilclass在Controller#show中。
这是我的表演行动
def show
@category = Category.find_by_url_name(params[:id])
@brands = @category.brands
@categories = Category.find(:all)
@meta_title = "#{@category.name}"
respond_to do |format|
format.html
@brand = @brands.first
@search = Product.search.order(params[:order] || 'descend_by_date')
@products = @search.paginate(:conditions => { :category_id => @category, :brand_id => @brand }, :page => params[:page])
render :template => 'brands/show'
format.xml { render :xml => @category }
end
end
浏览器跟踪信息:
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in `method_missing'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/active_record/named_scope_tools.rb:16:in `named_scope_options'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic- 2.5.8/lib/searchlogic/named_scopes/alias_scope.rb:54:in `named_scope_options'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/named_scopes/or_conditions.rb:14:in `named_scope_options'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/search/scopes.rb:15:in `scope_options'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/search/method_missing.rb:80:in `cast_type'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/search/method_missing.rb:22:in `method_missing'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/search/method_missing.rb:36:in `send'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/searchlogic-2.5.8/lib/searchlogic/search/method_missing.rb:36:in `method_missing'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:28:in `show'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:25:in `show'
它指向show动作行25和28,第25行是respond_to do | format |
第28行是@search - 产品系列。
更奇怪的是,如果我去编辑类别,则url:/ categories / amps / edit将返回以下内容:
ActiveRecord::RecordNotFound in CategoriesController#edit
Couldn't find Category with ID=amps
RAILS_ROOT: /Users/tjs/Sites/emeraldcg
Application Trace | Framework Trace | Full Trace
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-2.3.5/lib/active_record/base.rb:1586:in `find_one'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids'
/Users/tjs/.rvm/gems/ruby-1.8.7-p352/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'
/Users/tjs/Sites/emeraldcg/app/controllers/categories_controller.rb:50:in `edit'
我的分类模型:
class Category < ActiveRecord::Base
has_many :brands, :through => :products, :uniq => true
has_many :products
# acts_as_friendly_param :name
validates_presence_of :url_name
validates_uniqueness_of :url_name
def to_param
self.url_name
end
end
这个应用程序在rails 3之前运行良好...非常令人沮丧,我是一个较新的Rails开发人员,所以仍然试图找出如何返回并使用rails 2项目。
任何想法?
答案 0 :(得分:1)
您始终可以将@category = Category.find_by_url_name(params[:id])
替换为@category = Category.find(:first, :conditions => ["url_name = ?", params[:id]])
。
切换到Rails 3.1会更好;)
答案 1 :(得分:0)
“找不到ID = amps的类别”是尝试使用id查找记录的查询的结果。 例如:
User.find('amps')
will be converted into
SELECT * FROM users WHERE id='amps';
请确保您使用正确的代码来查找类别。
答案 2 :(得分:0)
正如@nkm指出的那样,当您的应用尝试从名为“amps”的ID中搜索类别时会发生此错误
检查您的show url并检查您的routes.rb文件以查看Category是否已配置为Resource
HTH
sameera