【发布时间】:2015-11-27 18:48:28
【问题描述】:
我有一个看起来像这样的表格
<%= simple_form_for @contact_form, url: contact_form_index_path, method: :post do |f| %>
<%= f.input :first_name, placeholder: 'Jane' %>
<%= f.input :last_name, placeholder: 'Doe' %>
<%= f.input :email, placeholder: 'example@email.com' %>
<%= f.input :address, placeholder: '123 Main Street' %>
<%= f.input :city, placeholder: 'Pleasantville' %>
<%= f.input :state, placeholder: 'NJ' %>
<%= f.input :zip_code, placeholder: '12345' %>
<%= f.input :phone, placeholder: '(123) 456-7890' %>
<%= f.button :submit %>
<% end %>
我的数据库架构如下所示;
create_table "contact_forms", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "address"
t.string "city"
t.string "zip_code"
t.string "state"
t.integer "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我的控制器如下所示;
class ContactFormController < ApplicationController
def new
@contact_form = ContactForm.new
end
def create
@contact_form = ContactForm.new
if @contact_form.save
redirect_to pages_url
else
render :new
end
end
end
当我测试表单并尝试将输入的数据发布到我的数据库中时,我没有收到错误,但数据没有填充到表中。
您知道是什么原因造成的吗?以下是日志,如果有帮助的话。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IniAUnB4O51+1KrNy5Ip/nVPqmEZaXopoFozaBu98bkvg0MP5GxbEkzKKltY/QuVomxE2hnDT7cIloy99u0Nsw==", "contact_form"=>{"first_name"=>"Jack", "last_name"=>"Burum", "email"=>"", "address"=>"", "city"=>"", "state"=>"", "zip_code"=>"", "phone"=>""}, "commit"=>"Create Contact form"}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "contact_forms" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-11-27 18:37:45.421056"], ["updated_at", "2015-11-27 18:37:45.421056"]]
(7.3ms) commit transaction
Redirected to http://localhost:3000/pages
Completed 302 Found in 11ms (ActiveRecord: 7.7ms)
【问题讨论】:
-
你能发布你的控制器吗
-
@Jon 发布了控制器
标签: ruby-on-rails forms sqlite model-view-controller simple-form