Mike Fair
Mike Fair

Reputation: 95

Running App from Heroku

I've built my rails app and pushed it over to Heroku to go live. The problem is I cannot create or update events. When trying to update or create I get the message that 'Something went wrong'. Here is the error in my heroku logs:

2015-10-23T13:38:50.014461+00:00 app[web.1]:    (1.8ms)  BEGIN
2015-10-23T13:38:50.039168+00:00 app[web.1]:    (1.9ms)  ROLLBACK
2015-10-23T13:38:50.043567+00:00 app[web.1]: 
2015-10-23T13:38:50.043570+00:00 app[web.1]: TypeError (no implicit conversion of Array into String):
2015-10-23T13:38:50.043571+00:00 app[web.1]:   app/controllers/events_controller.rb:19:in `create'
2015-10-23T13:38:50.043572+00:00 app[web.1]: 
2015-10-23T13:38:50.043572+00:00 app[web.1]: 2015-10 23T13:38:50.030951+00:00 app[web.1]:   SQL (2.4ms)  INSERT INTO "events" ("name", "description", "picture", "datestart", "dateend", "timestart", "timeend", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["name", "New Event w/Picture"], ["description", "adlkfjasdlkfjasjdkf\r\nadflkajsdfkajsdf\r\nadflkajdfa\r\ndfadf\r\nadfajsdfaf"], ["picture", "photo.jpg"], ["datestart", "2015-10-23"], ["dateend", "2015-10-23"], ["timestart", "2015-10-23 13:38:00.000000"], ["timeend", "2015-10-23 13:38:00.000000"], ["created_at", "2015-10-23 13:38:50.020179"], ["updated_at", "2015-10-23 13:38:50.020179"]]
2015-10-23T13:38:50.040280+00:00 app[web.1]: Completed 500 Internal Server Error in 696ms (ActiveRecord: 7.3ms)
WARNING: Toolbelt v3.42.20 update available.

I get the same error when I do an update to an existing event. However it references the 'update' action.

Here is the code for the create action in the events_controller.rb file:

def create
  @event = Event.new(event_params)

  if @event.save
    flash[:success] = "Your event was created successfully!"
    redirect_to events_path
  else
    render :new
  end
end

  def event_params
    params.require(:event).permit(:name, :description, :datestart, :dateend, :timestart, :timeend, :cost, :picture)
  end

Here is event.rb code:

class Event < ActiveRecord::Base
  default_scope { order('datestart ASC') }
    belongs_to :user
    has_many :regs

    mount_uploader :picture, PictureUploader
    validate :picture_size

    private
      def picture_size
        if picture.size > 5.megabytes
          errors.add(:picture, "should be less that 5MB")
        end
      end
end

I can run the app on the development side with no problem (Cloud9).

Any assistance would be most helpful.

Upvotes: 2

Views: 75

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Interesting... thanks for posting!

It doesn't look like the problem is with your code, more something in the backend (maybe the database column setup or something).


Debug

I see you've fixed it.

If an error like this appears again, there are several things you can do:

  1. Restart your server (maybe there was a caching issue or something). I believe you can do this with heroku restart -a app_name

  2. Check your DB -- pgsql is similar but slightly different to mysql. If you're storing "description" data, you'll probably be best using t.text when creating your schema

  3. Remove Image Uploads -- if you're using carrierwave, comment out the mount_uploader :picture, PictureUploader & validate :picture_size lines -- this will prevent the image from being processed, which could be an issue.


Either way, with Heroku, there are a number of small issues which can occur.

Although it's a great platform, you have to remember that you need a production db (& it won't have the same data as your dev db), as well as having to "warm up" (if you're on the free tier)

Upvotes: 1

fschuindt
fschuindt

Reputation: 841

I don't know, but have you tried?:

def create
  event_params[:description] = event_params[:description].to_s
  @event = Event.new(event_params)

  if @event.save
    flash[:success] = "Your event was created successfully!"
    redirect_to events_path
  else
    render :new
  end
end

I know it's ugly but it's just for test. Seems to me you're using SQLite in development and PostgreSQL in production, and this may causing this problem.

The description parameter is an Array originally? If so, explicit converting it to String may work. If it's other parameter you're trying to convert from Array to String, try to do in this way and see if it's work. I am not sure.

Upvotes: 0

Related Questions