开发者

undefined method error ^ in ruby on rails

开发者 https://www.devze.com 2023-03-09 19:17 出处:网络
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

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 w开发者_如何学Corks 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消