← Back to Upcase

File Uploads with Paperclip


(Upcase ) #1

© 2012 - 2017 thoughtbot, inc. Upcase, the design of a robot, and thoughtbot are registered trademarks of thoughtbot, inc.


This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/intermediate-rails-4

(Alwan Mortada) #2

I’m having a problem with when uploading an image.
undefined method `body’ for #Shout:0x007fd2e0cf3f68

def create
	shout =	current_user.shouts.create(shout_params)
	redirect_to root_path, redirect_option_for(shout)
end
private

def shout_params
	{content: content_from_params}
end

def content_from_params
	case params[:shout][:content_type]
		when "TextShout" then TextShout.new(text_shout_content_params)
		when "PhotoShout" then PhotoShout.new(photo_shout_content_params)
	end
end

def text_shout_content_params 
	params.require(:shout).require(:content).permit(:body)
end

def photo_shout_content_params 
	params.require(:shout).require(:content).permit(:image)
end

the error points to the shout_params


(Alwan Mortada) #3

@Upcase Anyone please help!


(Paul Smith) #4

@TheeGrassHopper It’s hard to tell from just that snippet, but it looks like your Shout model doesn’t have a body method because ActiveRecord may not have implemented it for you. This might mean that the database column for body is not there. If that’s the case, you’d need to generate a migration that adds body to the shouts table.

Can you paste your db/schema.rb so we can see what your database looks like? It would also be helpful if you posted the Shout code, or linked to a public repo of the code so it’s a bit easier to diagnose.


(Alwan Mortada) #5
 ActiveRecord::Schema.define(version: 20171012163136) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "photo_shouts", force: :cascade do |t|
t.string "image_file_name", null: false
t.string "image_content_type", null: false
t.integer "image_file_size", null: false
t.datetime "image_updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
  end

  create_table "shouts", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "content_type", null: false
t.integer "content_id", null: false
t.index ["content_id", "content_type"], name: "index_shouts_on_content_id_and_content_type"
t.index ["user_id"], name: "index_shouts_on_user_id"
  end

  create_table "text_shouts", force: :cascade do |t|
t.string "body", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", null: false
t.string "encrypted_password", limit: 128, null: false
t.string "confirmation_token", limit: 128
t.string "remember_token", limit: 128, null: false
t.string "username"
t.index ["email"], name: "index_users_on_email"
t.index ["remember_token"], name: "index_users_on_remember_token"
t.index ["username"], name: "index_users_on_username", unique: true
  end

  add_foreign_key "shouts", "users"
end

(Alwan Mortada) #6
class Shout < ApplicationRecord
  belongs_to :user
  belongs_to :content, polymorphic: :true
  validates :body, presence: true, length: {in: 1..144}
  validates :user, presence: true

  default_scope { order(created_at: :desc)}
  delegate :username, to: :user 

end

(Alwan Mortada) #7

@catalystmediastudios thank you for looking at this

class TextShout < ApplicationRecord
    	validates :body, presence: true, length: {in: 1..144}
    end

 class PhotoShout < ApplicationRecord
	has_attached_file :image, styles: { thumb: "200x200" }

	validates_attachment :image,
	content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] },
 	size: { in: 0..10.megabytes },
 	presence: true
end

(Paul Smith) #8

@TheeGrassHopper It’s a bit hard to tell, but I think it is because of the validates :body in Shout I think it only needs to be in the TextShout. can you remove the body validation in Shout and let me know if that works?

If that doesn’t can you paste the complete error message including the stack trace with the file names and line numbers?


(Alwan Mortada) #9

Thank you very much !! That worked. I can’t believe I missed that. I watched the video a bunch of time to see what I missed and that didn’t come up!! @catalystmediastudios


(Paul Smith) #10

You’re welcome. I’m glad that worked @TheeGrassHopper!


(STEVEN AGUILAR) #11

I get the following error when I try to post an image shout
No route matches [GET] "/shouts"