2014-08-29 58 views
1

我對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」]]「

實際上,它起初跑起來好了,但我不知道爲什麼突然不起作用。非常感謝你的幫助。

+0

模型上有驗證嗎? – 2014-08-29 11:23:20

+0

你可以添加你的視圖表單代碼嗎? – 2014-08-29 11:25:13

+0

非常感謝您的支持@ BroiSatse幫助我解決了這個問題。我也是SF的新手。當我想添加我的視圖代碼時,我不能點擊「保存」按鈕。所以對我表示感謝有點慢。非常感謝 – cosmoseeker 2014-08-29 11:41:49

回答

2
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 

刪除所有不必要的行。 Ruby方法返回它上一次執行的行返回的值 - 在這種情況下,它始終爲零(從puts返回的值)。

def energy_params 
    params.require(:energy).permit(:title, :capacity, :generation, :power_category, :location, :longitude, :latitude, :energy_role) 
end 
+0

好趕上!這裏的關鍵在於,如果方法'energy_params'的最後一行是'puts',則返回nil並且**不返回**,則從方法返回的值是**方法的最後一行** 'params.require' – 2014-08-29 11:30:37

+0

它確實有效。非常感謝你。其實,我原來的代碼就像你的代碼一樣。但正如我所提到的,我是一個新人。所以我想知道代碼的執行順序,所以我添加了一些提示。我真的沒有意識到問題在這裏。非常感謝 – cosmoseeker 2014-08-29 11:37:02