(对不起我的英语)如果我想用:name
和:date
参数上传一条新记录,这就是我发现的:
控制器:
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)