Michael
Michael

Reputation: 31

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

says the error is on line #33.

On line 33 I've got <%= form_for @try do |f| -%>

Upvotes: 1

Views: 118

Answers (4)

Shiv
Shiv

Reputation: 8412

If you are using the code in new.html.haml or new.html.erb view file

def new
 @try = ModelName.new
end

Upvotes: 0

Tim Sullivan
Tim Sullivan

Reputation: 16888

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.

Upvotes: 2

MrDanA
MrDanA

Reputation: 11647

The @try variable must be nil. How is it set? Are you certain it will always contain a valid object?

Upvotes: 1

nugget
nugget

Reputation: 168

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.

Upvotes: 0

Related Questions