我创建了一个 Rails 表单来创建一个包含以下字段的“事件”(嵌套在组资源中)。字段when
和exactwhen
(命名非常糟糕,我知道,我只是在创建一个快速演示)是指日期和时间
<%= form_for [@group, @event], url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= label_tag(:title, "Event Title") %>
<%= f.text_field :title %>
<%= label_tag(:description, "Event Description") %>
<%= f.text_area :description, size: "60x12" %>
<%= label_tag(:address, "Address") %>
<%= f.text_field :address %>
<%= label_tag(:when, "When") %>
<%= text_field_tag(:when) %>
<%= label_tag(:exactwhen, "exactwhen") %>
<%= text_field_tag(:exactwhen) %>
<%= f.submit "Create" %>
<% end %>
这些字段存在于数据库中的事件模型上
create_table "events", force: true do |t|
t.string "title"
t.text "description"
t.string "address"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "group_id"
t.date "when"
t.time "exactwhen"
end
但是,当我提交表单时,您会注意到它when
并exactwhen
没有包含在事件哈希中
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5kkL2a3yDHEINSqSg+1cXd708GtRPgymbKmASs+eih8=", "event"=>{"title"=>"First Wankers Meeting", "description"=>"get drunk", "address"=>"57 Robert Crescent"}, "when"=>"08/23/2013", "exactwhen"=>"12", "commit"=>"Create", "group_id"=>"38"}
when
和数据exactwhen
没有被插入到记录中
Group Load (0.6ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT 1 [["id", "38"]]
(0.2ms) BEGIN
SQL (2.0ms) INSERT INTO "events" ("address", "created_at", "description", "group_id", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["address", "57 Robert Crescent"], ["created_at", Sat, 03 Aug 2013 16:38:30 UTC +00:00], ["description", "get drunk"], ["group_id", 38], ["title", "First Wankers Meetup"], ["updated_at", Sat, 03 Aug 2013 16:38:30 UTC +00:00]]
我假设表单数据when
并exactwhen
没有被插入到数据库中,因为它们不在事件哈希中。如果是这种情况,您能否解释原因,或者解释我如何才能纠正这个问题?
这是 events_controller.rb 中的创建操作
def create
@group = Group.find(params[:group_id])
@event = @group.events.build(event_params)
if @event.save
....
private
def event_params
params.require(:event).permit(:title, :description, :when, :exactwhen, :address)
end