Question

I am trying to create an event within a method of a separate controller (from the model I am creating).

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

I get this error: undefined methodstringify_keys'`

I have a method that generates todays date, but I don't believe that is the issue since I've tested it with an actual date and still get the same stringify error.

Date is an attribute, here is my schema:

  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

I've tried using new_event.date but get the same error. The error gets highlighted in terminal on the following line:

@event = current_merchant.events.create(new_event)

The error that shows up in console is the following:

(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)
Was it helpful?

Solution

Update the checkin method as below:

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

You were trying to create an associated event record for current_merchant by passing an instance of Event model to create method. create method expects Hash as an argument which is why you get the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top