0

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)
3
  • date must be an attribute of Event model, new_event.date = todays_date(current_merchant) Commented Apr 24, 2014 at 18:57
  • Date is an attribute. And when I run it as date.event,m, I get the same stringify error Commented Apr 24, 2014 at 19:05
  • Share the full server log generated when you get this error. Add it in the question. Commented Apr 24, 2014 at 19:07

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This was my initial solution, and it removes the stringify error BUT the date is not being saved the the event.
I see that you are using Rails 4.0.4, you would need to white list the attributes that you would like to be saved.
What is "white list"?
Assuming that checkin method is in EventsController. See if you have a method name event_params in it. Share the code of that method in question.
The checkin method is not in events controller, it is in merchants controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.