Rails未选中的参数

时间:2013-04-25 08:07:49

标签: ruby-on-rails strong-parameters

我正在尝试添加包含字符串列表的记录。

模型:

class Startup < ActiveRecord::Base
  before_validation(:on => :create) do
    self.mal = false
  end
  serialize :category
  attr_accessible :name, :url, :description, :category, :creator, :mal, :founded
end

但我的日志中仍然出现此错误:

Started POST "/startups" for 127.0.0.1 at 2013-04-25 05:05:00 -0300
Processing by StartupsController#create as JS
  Parameters: {"utf8"=>"✓", "startup"=>{"name"=>"test", "url"=>"http://startupcrawler.com", "category"=>["", "Analytics", "Fitness", "Gaming"], "founded(1i)"=>"2013", "founded(2i)"=>"4", "founded(3i)"=>"1", "description"=>"fdsa"}, "commit"=>"Create Startup"}
Unpermitted parameters: category
  [1m[35m (0.2ms)[0m  BEGIN
  [1m[36m (0.2ms)[0m  [1mROLLBACK[0m
Completed 500 Internal Server Error in 85ms

我做错了什么?为什么不赢得类别序列化?

修改

刚刚发现这种方法已被弃用,我使用的是Rails 4 ......我应该使用什么呢?

编辑2:

现在在日志中得到这个:

Started POST "/startups" for 127.0.0.1 at 2013-04-25 05:30:33 -0300
Processing by StartupsController#create as JS
  Parameters: {"utf8"=>"✓", "startup"=>{"name"=>"blah", "url"=>"http://startupcrawler.com", "category"=>["", "Fitness"], "founded(1i)"=>"2013", "founded(2i)"=>"4", "founded(3i)"=>"1", "description"=>"fdsa"}, "commit"=>"Create Startup"}
  [1m[36m (0.2ms)[0m  [1mBEGIN[0m
  [1m[35m (0.5ms)[0m  ROLLBACK
  Rendered startups/_form.html.erb (16.1ms)
  Rendered startups/new.html.erb within layouts/application (18.9ms)
Completed 200 OK in 128ms (Views: 29.9ms | ActiveRecord: 0.8ms)

但没有创建新记录(ROLLBACK由于某种原因)。

我从模型中删除了serialize :category。现在在我的控制器中:

def startup_params
      params.require(:startup).permit(:name, :url, :added, :founded, :mal, :creator, :description, {:category => []})
end

1 个答案:

答案 0 :(得分:1)

出于这个原因,

Strong parameters内置于Rails 4中。