Ruby on Rails未定义方法`stringify_keys'

时间:2014-04-24 18:50:09

标签: ruby-on-rails model

我正在尝试在单独控制器的方法中创建一个事件(来自我正在创建的模型)。

def checkin
    #create a new event with todays date
    new_event = Event.new()
    new_event[:date] = todays_date(current_merchant)
    @event = current_merchant.events.create(new_event)
end

我收到此错误: undefined method stringify_keys'`

我有一种方法可以生成今天的日期,但我不相信这是问题,因为我已经用实际日期对其进行了测试,但仍然得到相同的字符串错误。

日期是属性,这是我的架构:

  create_table "events", force: true do |t|
    t.string   "name"
    t.date     "date"
    t.integer  "age"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "merchant_id"
    t.string   "photo"
  end

我已尝试使用new_event.date,但收到同样的错误。 错误在以下行的终端中突出显示:

@event = current_merchant.events.create(new_event)

控制台中显示的错误如下:

(0.3ms)  BEGIN
(0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 4495ms

NoMethodError (undefined method `stringify_keys' for #<Event:0x007f7f8f2a26f8>):
  app/controllers/merchants_controller.rb:25:in `checkin'


  Rendered /Users/steclaudinodaffara/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
  Rendered /Users/steclaudinodaffara/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
  Rendered /Users/steclaudinodaffara/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /Users/steclaudinodaffara/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (14.5ms)

1 个答案:

答案 0 :(得分:1)

更新checkin方法,如下所示:

def checkin
    params = ActionController::Parameters.new({:event => {date: todays_date(current_merchant}})
    @event = current_merchant.events.create(params.require(:event).permit(:date)))
end

您尝试通过将event record模型的实例传递给create方法来为current_merchant创建关联的Eventcreate方法期望Hash作为参数,这就是您收到错误的原因。