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