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):
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 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
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: {...}
...
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.
精彩评论