未定义的方法“状态”;对于 nil:nilclass
好吧,奇怪的错误。一切都工作正常..但现在不行了。
目前我有一个简单的多对一关联。
路线设置如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有一个类似的问题 - 我有一个名为“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.
@app was not found, you can fetch forms inside try
如果你的模板处理@app,并且拥有它很重要,最好处理异常:
@app was not found, you can fetch forms within try
If your template handles the @app, and it's important to have it, better to handle the exception:
首先,请检查 log/development.log 或在浏览器上。它应该可以帮助您找到错误发生的地方。
接下来,params 值是什么?检查日志/development.log。它可能如下所示:
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:
对于内部服务器错误,您的应用程序无法正确启动。您应该检查服务器错误日志。它可能会让您了解问题所在。如果您或团队成员(视情况而定)没有做出任何破坏应用程序的更改,那么您应该与您的主机核实。也许他们对您的环境进行了更改,从而导致了错误。
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.