未定义的方法“状态”;对于 nil:nilclass

发布于 2024-12-03 14:46:14 字数 1328 浏览 0 评论 0原文

好吧,奇怪的错误。一切都工作正常..但现在不行了。

目前我有一个简单的多对一关联。

路线设置如下:

resources :apps do
  resources :forms
end

应用程序:

has_many :forms

表单:

belongs_to :app

Forms_controller 索引操作:

def index
  @app = App.find(params[:app_id])
  @forms = @app.forms
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @forms }
  end
end

我已经从 forms.html.erb 布局文件中取出了所有代码/html,因此它应该呈现一个空白页面。

相反,我收到此错误:

nil:NilClass status 的未定义方法“

status”甚至没有在我的应用程序

帮助中的任何位置定义,我们将不胜感激。

编辑:

这是我的development.log 文件中显示的内容

Started GET "/apps/4/forms" for 127.0.0.1 at 2011-09-05 23:14:16 -0700
  Processing by FormsController#index as HTML
  Parameters: {"app_id"=>"4"}
  [1m[36mApp Load (0.1ms)[0m  [1mSELECT "apps".* FROM "apps" WHERE "apps"."id" = ? LIMIT 1[0m  [["id", "4"]]
  [1m[35m (0.1ms)[0m  SELECT COUNT(*) FROM "forms" WHERE "forms"."app_id" = 4
0
  [1m[36mForm Load (0.1ms)[0m  [1mSELECT "forms".* FROM "forms" WHERE "forms"."app_id" = 4[0m
Rendered forms/index.html.erb within layouts/forms (1.2ms)
Completed 500 Internal Server Error in 37ms

NoMethodError (undefined method `status' for nil:NilClass):

Ok weird error. Everything was working fine.. and now its not.

Currently I have a simple many to one association.

Route is set up like this:

resources :apps do
  resources :forms
end

App:

has_many :forms

Form:

belongs_to :app

Forms_controller index action:

def index
  @app = App.find(params[:app_id])
  @forms = @app.forms
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @forms }
  end
end

I've taken every bit of code/html out of the forms.html.erb layout file so it should be rendering a blank page.

Instead I'm getting this error:

undefined method `status' for nil:NilClass

status isn't even defined anywhere in my app

help would be greatly appreciated.

EDIT:

Here is what is displayed in my development.log file

Started GET "/apps/4/forms" for 127.0.0.1 at 2011-09-05 23:14:16 -0700
  Processing by FormsController#index as HTML
  Parameters: {"app_id"=>"4"}
  [1m[36mApp Load (0.1ms)[0m  [1mSELECT "apps".* FROM "apps" WHERE "apps"."id" = ? LIMIT 1[0m  [["id", "4"]]
  [1m[35m (0.1ms)[0m  SELECT COUNT(*) FROM "forms" WHERE "forms"."app_id" = 4
0
  [1m[36mForm Load (0.1ms)[0m  [1mSELECT "forms".* FROM "forms" WHERE "forms"."app_id" = 4[0m
Rendered forms/index.html.erb within layouts/forms (1.2ms)
Completed 500 Internal Server Error in 37ms

NoMethodError (undefined method `status' for nil:NilClass):

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生一个溪 2024-12-10 14:46:14

我有一个类似的问题 - 我有一个名为“response”的方法,Rails 内部的某些东西正在调用“status”,并且它同样在没有堆栈跟踪的情况下退出。

对于名为“app”和“forms”的东西,您可能会遇到类似的情况。

I had a similar issue - I had a method named 'response' that something internal to Rails was calling 'status' on and it similarly bailed without a stack trace to speak of.

With things named 'app' and 'forms' you might be running into something similar.

水染的天色ゝ 2024-12-10 14:46:14

@app was not found, you can fetch forms inside try

def index
  @app = App.find(params[:app_id])
  @forms = @app.try(:forms)
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @forms }
  end
end

如果你的模板处理@app,并且拥有它很重要,最好处理异常:

def index
  @app = App.find!(params[:app_id]) # raise an exception until find
  @forms = @app.forms
rescue
  flash[:error] = "App not found!"
end

@app was not found, you can fetch forms within try

def index
  @app = App.find(params[:app_id])
  @forms = @app.try(:forms)
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @forms }
  end
end

If your template handles the @app, and it's important to have it, better to handle the exception:

def index
  @app = App.find!(params[:app_id]) # raise an exception until find
  @forms = @app.forms
rescue
  flash[:error] = "App not found!"
end
老子叫无熙 2024-12-10 14:46:14

首先,请检查 log/development.log 或在浏览器上。它应该可以帮助您找到错误发生的地方。

接下来,params 值是什么?检查日志/development.log。它可能如下所示:

...
Processing FormsController#index (for 127.0.1.1 at YYYY-MM-DD hh:mm:ss) [GET]
Parameters: {...}
...

First, please check log/development.log or on browser. It should help you where the error hapened.

Next, what is params value? Check log/development.log. It may look like the followings:

...
Processing FormsController#index (for 127.0.1.1 at YYYY-MM-DD hh:mm:ss) [GET]
Parameters: {...}
...
故事灯 2024-12-10 14:46:14

对于内部服务器错误,您的应用程序无法正确启动。您应该检查服务器错误日志。它可能会让您了解问题所在。如果您或团队成员(视情况而定)没有做出任何破坏应用程序的更改,那么您应该与您的主机核实。也许他们对您的环境进行了更改,从而导致了错误。

For internal server errors your application could not be started properly. You should check your server error log. It may give you insight as to what the problem is. If you, or a team member as the case may be, didn't make any changes that broke the application then you should check with your host. Perhaps they made changes to your environment that are causing an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文