虽然可以使用params,但无法通过" XXX_params"创建

时间:2014-08-29 11:19:29

标签: ruby-on-rails ruby ruby-on-rails-4

我是Ruby on Rails的新手。我遇到了以下问题: 我的rails环境是ruby2.0.0,rails 4.1.1

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看起来像正常运行,但是当代码时 &#34; @energy = Energy.create(energy_params)&#34; 运行,它将被翻译为SQL: &#34; SQL(0.4ms)INSERT INTO&#34; energy&#34; (&#34; created_at&#34;,&#34; updated_at&#34;)VALUES(?,?)[[&#34; created_at&#34;,&#34; 2014-08-29 10:44:25.440007 &#34;],[&#34; updated_at&#34;,&#34; 2014-08-29 10:44:25.440007&#34;]]&#34;

实际上,它起初没问题,但我不知道它为什么突然不起作用。非常感谢你的帮助。

1 个答案:

答案 0 :(得分: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方法返回其最后执行的行返回的值 - 在这种情况下,它总是为nil(从puts返回的值)。

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