我對Ruby的更新上Rails.I有一個問題如下: 我的Rails環境ruby2.0.0,導軌4.1.1不能用「XXX_params」創建,雖然PARAMS可
1,首先,這裏是我的精力控制器:
class EnergiesController < ApplicationController
before_action :all_energies, only: [:index, :create, :update, :destroy]
before_action :set_energies, only: [:edit, :update, :destroy]
respond_to :html, :js
........
def create
puts "in create method"
@energy = Energy.create(energy_params)
puts "create method end"
end
.......
private
.......
def energy_params
puts "in get_params method"
params.require(:energy).permit(:title, :capacity, :generation, :power_category, :location, :longitude, :latitude, :energy_role)
puts params
puts "get_params method end"
end
end
2,我的日誌輸出是:
Started POST "/energies" for 127.0.0.1 at 2014-08-29 18:44:25 +0800
Processing by EnergiesController#create as JS
Parameters{"utf8"=>"✓","authenticity_token"=>"rliBtHNMG/7PvivOE3WT4Z2K0MY/d9WnUYRFu04imNk=", "energy"=>{"title"=>"qwe", "capacity"=>"1", "generation"=>"12", "power_category"=>"12", "location"=>"12", "longitude"=>"12", "latitude"=>"123", "energy_role"=>"13"}, "commit"=>"Create Energy"}
in get_all method
get_all method end
**in create method**
**in get_params method**
{"utf8"=>"✓", "authenticity_token"=>"rliBtHNMG/7PvivOE3WT4Z2K0MY/d9WnUYRFu04imNk=", "energy"=>{"title"=>"qwe", "capacity"=>"1", "generation"=>"12", "power_category"=>"12", "location"=>"12", "longitude"=>"12", "latitude"=>"123", "energy_role"=>"13"}, "commit"=>"Create Energy", "action"=>"create", "controller"=>"energies"}
**get_params method end**
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "energies" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-08-29 10:44:25.440007"], ["updated_at", "2014-08-29 10:44:25.440007"]]
(171.5ms) commit transaction
**create method end**
in create_js file
Energy Load (0.3ms) SELECT "energies".* FROM "energies"
Rendered energies/_energy.html.erb (4.2ms)
Rendered energies/create.js.erb (11.4ms)
Completed 200 OK in 195ms (Views: 17.9ms | ActiveRecord: 172.4ms)
,你可以看到,該方法get_param貌似正常運行,但是當代碼 「@energy = Energy.create(energy_params)」運行,它將被轉換爲SQL: 「SQL(0.4ms)INSERT INTO」energies「(」created_at「,」updated_at「)VALUES(? ?)[[「created_at」,「2014-08-29 10:44:25.440007」],[「updated_at」,「2014-08-29 10:44:25.440007」]]「
實際上,它起初跑起來好了,但我不知道爲什麼突然不起作用。非常感謝你的幫助。
模型上有驗證嗎? – 2014-08-29 11:23:20
你可以添加你的視圖表單代碼嗎? – 2014-08-29 11:25:13
非常感謝您的支持@ BroiSatse幫助我解決了這個問題。我也是SF的新手。當我想添加我的視圖代碼時,我不能點擊「保存」按鈕。所以對我表示感謝有點慢。非常感謝 – cosmoseeker 2014-08-29 11:41:49