Ruby on rails Rails 2.3.5获取未定义的方法“to#sym';零级:零级

Ruby on rails Rails 2.3.5获取未定义的方法“to#sym';零级:零级,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在运行一个rails 2.3.5应用程序,在一个控制器秀中得到一个nil:nil类 这是我的表演 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|

我正在运行一个rails 2.3.5应用程序,在一个控制器秀中得到一个nil:nil类

这是我的表演

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'
它指向ShowAction第25行和第28行,第25行是respond_to do格式|

第28行是@search-Product行

更奇怪的是,如果我去编辑类别,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
类别:产品,:uniq=>正确
有很多:产品
#作为友好的参数:名称
验证是否存在:url\u名称
验证以下项的唯一性:url\u名称
def至_参数
self.url\u名称
结束
结束
在rails 3之前,此应用程序运行良好。。。令人沮丧的是,我是一个较新的Rails开发人员,所以仍在试图找出如何返回并使用Rails2项目

有什么想法吗?

“找不到ID=amps的类别”是试图使用ID查找记录的查询结果。 例如:


请确保您使用了正确的代码来查找类别。

正如@nkm所指出的,当您的应用程序试图从名为“amps”的id中搜索类别时,会发生此错误

检查show url和routes.rb文件,查看类别是否已配置为资源


sameera

您始终可以使用
@category=category.find(:first,:conditions=>[“url\u name=?”,params[:id]])替换
@category=category.find(:first,:conditions=>,“url\u name=?”,params[:id]])


切换到Rails 3.1会更好;)

用户不参与@category=category.find_by_url_name(params[:id])是一种通过url_name查找类别的方法,该方法验证存在性:url_name验证唯一性:url_name def to_param self.url_name endWell我在这里指定了用户作为示例。按照rails约定,restful资源/categories/amps/edit映射到/categories/:id/edit。所以我怀疑你是在编辑操作中使用Category.find(params[:id]),还是在什么地方。哦,好吧,我想我明白你的意思了。。。对不起,用户感到困惑。My categories表有一个url_name字段,允许管理员创建一个更清晰的url结构,这就是def to_param self.url_name end方法发挥作用的地方,@category实例变量也从url_name中提取。。。谢谢你的帮助。我还不完全明白,但你们给我指出了正确的方向。
User.find('amps')
will be converted into 
SELECT * FROM users WHERE id='amps';