2011-11-21 21 views
0
class ItemController < ApplicationController 

    def create 
    item = current_user.items.build(params[:presentstem]) 
    item.created_at = Time.now 
    item.save! 
    redirect_to root_path 
    end 

    def destroy 

    end 

end 

和我的意見/家庭/索引/ html.erb形式添加項目Rails的錯誤 「預期... model.rb定義模式」

<div id="add_item"> 
    <p>Add a new item</p> 
    <% form_for Item.new do |f| %> 
     <div id="add_item_container"> 
     <%= f.text_field :present %> 
     <%= f.text_field :stem %> 
     <%= f.text_field :secondary %> 
     <%= f.check_box :atype %> 
     <%= f.text_field :comment %> 
     </div> 
     <%= f.submit "Add to List" %> 
    <% end %> 
</div> 

如何定義Item

位於localhost:3000,我得到

預計/Users/user/Desktop/test/app/models/item.rb定義項目
提取的源(左右線#3):

回答

1

你應該有這個文件/Users/user/Desktop/test/app/models/item.rb項目類的定義,也許你不...

class Item < ActiveRecord::Base 
    #class definition goes here 
end 
0

你在想錯。

你必須在app /模型的模型/ item.rb的

爲此,你必須在應用程序/控制器的控制器/ items_controller.rb

,你必須在應用程序/視圖/項目的意見/ template.haml

如果你想做一個form_for你做一個對象的形式。 rails正在尋找你想要參與的動作類型(new,update)並自動生成路由(restful)。

所以你只給了一個對象到的form_for助手

#in view 
=form_for Item.new do |f| 

#in items_controller.rb 
def new 
    @item = Item.new 
end 

#in new.haml 
=form_for @item