我搜索了並且沒有找到任何東西,所以我需要請你們幫忙。form_for未向數據庫提交字段
我有一個簡單的新窗體,應該從form_for中獲取兩個字段,然後將其發佈到數據庫中。問題在於它沒有。
我的示例數據庫只有兩個字段:姓名和SITE_ID
這裏是我的new.html.erb:
<%= form_for @kejsu do |f| %>
<%= f.text_field :name %>
<%= f.text_field :site_id %>
<%= f.submit "Create" %>
<% end %>
這裏的控制器:
def new
@kejsu = Kejs.new
end
def create
@kejsu = Kejs.new(params[:kejsu])
if @kejsu.save
redirect_to kejs_index_path
else
render "new"
end
end
命中率只有提交按鈕後插入時間戳。下面是從片斷軌服務器:
Started POST "/cases" for 192.168.56.1 at 2013-12-10 23:11:03 +0000
Processing by KejsyController#create as HTML
Parameters: {"utf8"=>"â", "authenticity_token"=>"Sr2ssiwtRtk9pRT5VfuDFglsEmGnjzwVkRGGBSb2zhA=", "kejs"=> {"name"=>"aa", "site_id"=>"3"}, "commit"=>"Create"}
(0.1ms) begin transaction
SQL (5.1ms) INSERT INTO "kejs" ("created_at", "updated_at") VALUES (?, ?) [["created_at", Tue, 10 Dec 2013 23:11:03 UTC +00:00], ["updated_at", Tue, 10 Dec 2013 23:11:03 UTC +00:00]]
(4.0ms) commit transaction
Redirected to http://192.168.56.101:3000/cases
Completed 302 Found in 14ms (ActiveRecord: 9.2ms)
正如你可以看到這些字段作爲參數傳遞,但INSERT不會插入它們。
我試過用默認的寧靜路由,寫我自己的路線,它不工作任何方式。
作爲獎勵,我的路線:
get 'cases' => 'kejsy#index', as: :kejs_index
get 'cases/new' => 'kejsy#new', as: :new_kejs
post 'cases' => 'kejsy#create'
包含模型也是有幫助的。它看起來像你的行動正在接收參數正確的問題是符合Kejsu。new(params [:kejsu]) – amitamb
我的模型非常空,只是通過rails g創建的標準聲明。 – toxicbeaver