Tuler
Tuler

Reputation: 19

Ruby on rails undefined variable

I am writing a new app and used an old app as kind of a reference on how to do things I am creating the new page and using the form_for

<% form_for(store) do |f| %>

<% end %>


def new
  @store = Store.new
end

Above is my controller did it the same way I did in my last app and getting this error. I am getting this error that is puzzling me because it works on other applications I made.

undefined local variable or method `store' for #<#<Class:0x007fb26b3634c0>:0x007fb26a427ce0>
Did you mean?  @store

I must be missing something conceptually.

Upvotes: 0

Views: 649

Answers (3)

tkz79
tkz79

Reputation: 95

I think you're missing something simple...

there was a change in the recent version of rails that passes a local variable to the partial that contains the form_for... and in doing so, removes the need for the @ symbol in the partial view. You can fix your error by adding an @ symbol before the store in the form for, or by creating the "translation" of the global variable to the local one... (excuse the terminology, i'm self taught).

<%= render 'form', store: @store %>

Upvotes: 1

Bartosz Grochowski
Bartosz Grochowski

Reputation: 51

You created variable @store, that's why ruby doesn't know what is store

<% form_for(@store) do |f| %>

You should read error messages

undefined local variable or method `store' ... Did you mean? @store

Upvotes: 5

Sujith Sudersan
Sujith Sudersan

Reputation: 11

Use @store in view.

<% form_for(@store) do |f| %>

<% end %>

Upvotes: 0

Related Questions