2011-05-18 41 views
-1
NoMethodError in CarController#add 

undefined method `user_id=' for #<Car:0x7160c70> 
RAILS_ROOT: C:/Users/Jatinder/BitNami RubyStack projects/mercedes_mod 2 
add.html (for adding car) 

add.html在/視圖/汽車NoMethodError在CarController#添加未定義的方法`USER_ID =」爲#<汽車:0x71451d8>

<h1>Ask a Question or Discuss Your Car</h1> 
<%= error_messages_for :car %> 
<br> 
<p>You can ask anything related to cars even if its not a Mercedes!</p> 
<% form_for :car do |f| %> 

    <p> 
    <%= f.label :name, "Title of Question" %> 
    <%= f.text_field :name %> 
    </p> 
    <p> 
    <%= f.label :description, "Describe Your Question" %> 
    <%= f.text_area :description %> 
    </p> 
    <p> 
    <%= f.submit "Add" %> 
    </p> 
<% end %> 

在car_controller.rb DEF添加:

def add 
     @title = "Ask a New Question" 
    if request.post? 
     @car = Car.new(params[:car]) 
     @car.user_id = User.logged_in(session).id 
    if @car.save 
     flash[:notice] = "Car #{@car.name} added!" 
     redirect_to :controller => :car, :action => :index 
     end 
    end 
    end 

car.rb型號:

class Car < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :subject 
    validates_presence_of :name, :description 
end 

的routes.rb

map.connect ':controller/:action/:id' 
    map.connect ':controller/:action/:id.:format' 
    map.resources :car, :users => { :delete => :get } 
    map.root :controller => "main" 
    map.root :controller => "car", :action => "destroy" 
end 

create_cars遷移:

class CreateCars < ActiveRecord::Migration 
    def self.up 
    create_table :cars do |t| 
     t.interger :user_id 
     t.string :name 
     t.string :description 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :cars 
    end 
end 
+0

將來你能否對你的問題做更多的描述?我認爲它的粗魯只是立即發佈所有的代碼,並期望我們調試它。也就是說,你能否告訴我們,在你的遷移中糾正錯誤之後,你會得到什麼其他錯誤?嘗試運行rake db:drop和rake db:遷移併發布錯誤。 – 2011-05-19 04:52:18

回答

0

您的遷移有一個錯字。 ,而不是t.interger您的:user_id。此外,請確保在運行您的應用程序之前在您的控制檯中運行rake db:migrate

+0

我完成了更改並運行了rake db:遷移仍然是相同的錯誤 – 2011-05-18 22:13:36

+0

假設您已經運行過它,您可能需要運行'rake db:migrate:redo'。 – 2011-05-18 22:17:35

+0

不,我剛剛也跑了,它完全遷移,但仍然沒有去同樣的錯誤? @dmarkow – 2011-05-18 22:19:59

0

你的遷移破獲:

t.interger :user_id 

你正在尋找一個整數

+0

我改變了它仍然沒有去 – 2011-05-18 22:15:32

相關問題