【问题标题】:Rails NoMethodError in Classified#show分类中的Rails NoMethodError#show
【发布时间】:2012-11-11 16:11:12
【问题描述】:

我正在关注一本名为 Rails Solutions: Rails Made Easy 的书,它是为 rails 2 编写的,但我使用的是 rails 3,这让事情变得非常有趣,让我学到了很多东西,这很好,但我我坚持上述问题。我在 Stack 和其他网站上阅读过,我认为这可能是路由问题,但到目前为止,这本书还没有提到任何关于路由的内容。

routes.rb

List::Application.routes.draw do
  match ':controller(/:action(/:id))(.:format)'
end

app/views/classified/show.html.erb

  <p>
    <strong>Title: </strong> <%= @classified.title %><br />
  </p>

app/controllers/classified_controller.rb

class ClassifiedController < ApplicationController
  def list
    @classifieds = Classified.find(:all)
  end
  def show
     @classifieds = Classified.find(params[:id])
  end
   def new
@classified = Classified.new
  end
  def create
    @classified = Classified.new(params[:classified])
    if @classified.save
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end
  def edit

  end
  def update

  end
  def delete

  end
end

Classified#show 中的 NoMethodError

显示 /home/mark/Documents/RoR/list/app/views/classified/show.html.erb 其中第 3 行出现:

nil:NilClass 的未定义方法 `title' 提取的源代码(第 3 行附近):

1: 2:

3:标题:
4:

【问题讨论】:

  • 你会经常遇到这个错误。 undefined method 'title' for nil:NilClass 表示您调用 title 的对象为零。在这里,它是@classified,在 Tomdarkness 的回答中了解原因

标签: ruby-on-rails ruby routes


【解决方案1】:

在您的控制器中,您使用了@classifieds,但在您看来,您使用了@classified。更改一个以匹配另一个。

【讨论】:

  • 谢谢,很奇怪,看着他的控制器,他混合了每种方法的分类和分类,有点混乱,我不知道为什么,但还是谢谢你。
猜你喜欢
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
相关资源
最近更新 更多