Reputation: 21
I'm trying out rails for the first time and I keep getting this undefined method error. All I did so far was just use rails to create a new project and the scaffold to create my database table called test. After applying the rate command everything works but when i run it in a web browser it gives me this error:
ActionView::TemplateError (undefined method `^' for "7":String) on line #3 of app/views/tests/new.html.erb:
1: <h1>New test</h1>
2:
3: <% form_for(@test) do |f| %>
4: <%= f.error_messages %>
5:
6: <p>
Anyone with any ideas on how I can deal with this error?
EDIT:-
"new" action in controller:-
def new
@test = Test.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @test }
end
end
Upvotes: 2
Views: 754
Reputation: 370435
The error message complains because Rails tried to use the ^
operator on the string "7", which doesn't support that operation (the number 7 would support that operation). The line indicated in the backtrace is form_for(@test)
, so form_for
does something to @test
which causes ^
to be called on 7
. The most likely reason for that is that Rails tries to xor @test
's id somewhere and unexpectedly @test.id
returns the string "7"
instead of the number 7
.
In other words you probably set up the tests
table to have an id column with a string type instead of integer, which Rails does not like.
Upvotes: 2
Reputation: 17790
My guess is that you have assigned something to @test
that isn't a model object. Please post the code for the new
method from your controller so we can dig into this further.
Upvotes: 1