0

(对不起,我的英语) 如果我想上传一个新的纪录,:name:date PARAMS,那是什么,我发现:回报率:.create尼尔斯

控制器:

class ActorController < ApplicationController 
    def index 
    end 

    def new 
     @actor = Actors.create 
    end 

    def create 
     @actor = Actors.create(params[:actors]) 
     if @actor.save 
     redirect_to actor_path, :notice => "Your actor was saved." 
     else 
     render "new" 
     end 
    end 
    end 

型号:(actors.rb)

class Actors < ActiveRecord::Base 
    attr_accessible :birth, :name 
    end 

和视图:(new.html.erb)

<%= form_for(@actor) do |a| %> 
    <%= a.text_field :name %> 
    <%= a.text_field :birth %> 
    <%= a.submit %> 
<% end %> 

而我在本地服务器控制台输出为:

Started PUT "/actor/40" for 127.0.0.1 at 2013-03-27 13:38:15 +0100 
Processing by ActorController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"BKhrP1Rfkco7r05wPT758M3CHQXRP5l5jcul77oTLPw=", "actors"=>{"name"=>"Bunny", "birth"=>"19/21/21"}, "commit"=>"Update Actors", "id"=>"40"} 
    (1.2ms) begin transaction 
    SQL (0.7ms) INSERT INTO "actors" ("birth", "created_at", "name", "updated_at") VALUES (?, ?, ?, ?) [["birth", nil], ["created_at", Wed, 27 Mar 2013 12:38:15 UTC +00:00], ["name", nil], ["updated_at", Wed, 27 Mar 2013 12:38:15 UTC +00:00]] 
    (197.8ms) commit transaction 
    (0.2ms) begin transaction 
    (0.1ms) commit transaction 
Redirected to http://localhost:3000/actor/40 
Completed 302 Found in 209ms (ActiveRecord: 200.0ms) 

回答

3

请编辑如下:

1)编辑模型:

class Actor < ActiveRecord::Base # Class name should be Singular 
    attr_accessible :birth, :name 
end 

2)编辑控制器:

class ActorsController < ApplicationController # Note Here the controller name should be plural without space 
    def index 
    end 

    def new 
     @actor = Actor.new # In the new action, it should be classname.new not create 
    end 

    def create 
     @actor = Actor.create(params[:actor]) # Here also the Actor class name should be singular 
     if @actor.save 
     redirect_to actor_path, :notice => "Your actor was saved." 
     else 
     render "new" 
     end 
    end 
    end 

PS:

1)在查看文件夹名称应该是Plural,所以你的文件夹名称是app/view/actors2)更改您的控制器名称,如actors_controller3)在你的路线,它应该是resources :actors4)您需要用正确的降低排列代码以找到您开始的位置和结束位置。这是开始编码的好方法。它会解决你的50%问题,找出你做错的地方。

+0

末-1但 当我解决它之后,它使另一个错误: 未初始化的常数ActorController :: Actor – 2013-03-27 12:51:22

+0

查看我的更新回答。在控制器中它应该是复数。所以,'ActorsController :: ApplicationController' – Vinay 2013-03-27 12:53:52

+0

期望/home/ben/blog/app/controllers/actor_controller.rb来定义ActorController – 2013-03-27 12:58:06

0

在你的控制器动作,你打错参数名:

Actors.create(params[:movie]) should be Actors.create(params[:actors]) 
+0

,你应该命名类的单数形式=>演员,不是演员 – Lichtamberg 2013-03-27 12:42:43

0

更改您的新的行动代码:

@actor = Actors.new 

并确认您的模型类是Actors 它可能有效。

0

很多的东西去错在这里:

  1. 控制器名称应该是多元的,而你将不得不重命名文件是actors_controller.rb

  2. 型号名称应为单数,也见您将文件的名称更改为actor.rb

  3. 新行为应该是@actor = Actors.new而不是create,#create是#new和#save的较短版本。

  4. params哈希表应该是params[:actor]

class ActorsController < ApplicationController 
    def index 
    end 

    def new 
     @actor = Actor.new 
    end 

    def create 
     @actor = Actor.create(params[:actor]) 
     if @actor.save 
      redirect_to actor_path, :notice => "Your actor was saved." 
     else 
      render "new" 
     end 
    end 
    end 

在Actor.rb也改变了型号名称,并确保这是该模型的名称。 同样在这种情况下,您应该签出您的迁移。

class Actor < ActiveRecord::Base 
    attr_accessible :birth, :name 
    end 
+0

未初始化的常量ActorController – 2013-03-27 12:55:06

+1

您还必须更改文件名。它应该是actors_controller.rb – Zippie 2013-03-27 12:55:47

0

@actor = Actors.create是错误的,因为它将@actor变量分配给新创建的Actors对象。这就是为什么你在输出中看到Processing by ActorController#update as HTML,因为该对象已经创建并正在尝试更新它。

你应该这样顺便说一句改为@actor = Actors.new