Creating a form Ruby on Rails
I'm trying to create a form on my website but I get this error:
undefined method 'model_name' for NilClass:Class
say开发者_开发问答s the error is on line #33.
On line 33 I've got <%= form_for @try do |f| -%>
A view does not exist in isolation. You need to have your controller set up all the variables that you might need in the view.
So, assuming this is an edit view, you'll need to have code in your controller's edit
action, something like this:
def edit
@try = SomeModel.find params[:id]
end
This will set up the @try variable and provide it to the view.
The @try
variable must be nil. How is it set? Are you certain it will always contain a valid object?
You need to link your form to a specific model. In your controller, what code is defining @try? Whatever it is seems to be failing to specify a new or current model instance.
If you are using the code in new.html.haml or new.html.erb view file
def new
@try = ModelName.new
end
精彩评论