解決したいこと
投稿画像をデータベースに保存し、表示させたい。
起こっている問題
エラーが発生し、保存されない。
TypeError in PostsController#create
can't quote ImageUploader
https://i.gyazo.com/eea2a77bdc9c97863db8e48d4c38a23b.png
application_controller.rb
ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :configure_permitted_parameters, if: :devise_controller? 4 5 protected 6 def configure_permitted_parameters 7 added_attrs = [:nickname, :email, :password, :birthday, :prefecture_id, :avatar_image] 8 devise_parameter_sanitizer.permit :sign_up, keys: added_attrs 9 devise_parameter_sanitizer.permit :sign_in, keys: added_attrs 10 devise_parameter_sanitizer.permit :account_update, keys: added_attrs 11 end 12 13end
posts_controller.rb
ruby
1class PostsController < ApplicationController 2 before_action :set_post, only: [:edit, :show] 3 before_action :move_to_index, except: [:index, :show] 4 5 def index 6 end 7 8 def new 9 @post = Post.new 10 end 11 12 def create 13 @post = Post.new(post_params) 14 if @post.save! 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 def destroy 22 post = Post.find(params[:id]) 23 post.destroy 24 end 25 26 def edit 27 end 28 29 def update 30 post = Post.find(params[:id]) 31 post.update(post_params) 32 end 33 34 def show 35 end 36 37 private 38 def post_params 39 params.require(:post).permit(:tag_id, :title, :text, :image).merge(user_id: current_user.id) 40 end 41 42 def set_post 43 @post = Post.find(params[:id]) 44 end 45 46 def move_to_index 47 redirect_to action: :index unless user_signed_in? 48 end 49 50end
model/post.rb
ruby
1class Post < ApplicationRecord 2 validates :tag_id, :title, :text, :image, presence: true, uniqueness: true 3 4 belongs_to :user 5 has_many :comments, dependent: :destroy 6 has_many :tags, through: :post_tags 7 has_many :post_tags, dependent: :destroy 8 9 mount_uploader :image, ImageUploader 10end
migrate/****_create_posts.rb
ruby
1class CreatePosts < ActiveRecord::Migration[5.2] 2 def change 3 create_table :posts do |t| 4 t.string :title, null: false 5 t.text :text, null: false 6 t.integer :tag_id 7 t.string :image 8 t.references :user, foreign_key: true 9 t.timestamps 10 end 11 end 12end
image_uploader.rb
ruby
1class ImageUploader < CarrierWave::Uploader::Base 2 # Include RMagick or MiniMagick support: 3 # include CarrierWave::RMagick 4 include CarrierWave::MiniMagick 5 6 # Choose what kind of storage to use for this uploader: 7 storage :file 8 # storage :fog 9 10 # Override the directory where uploaded files will be stored. 11 # This is a sensible default for uploaders that are meant to be mounted: 12 def store_dir 13 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 14 end 15 16 # Provide a default URL as a default if there hasn't been a file uploaded: 17 # def default_url(*args) 18 # # For Rails 3.1+ asset pipeline compatibility: 19 # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 20 # 21 # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 22 # end 23 24 # Process files as they are uploaded: 25 # process scale: [200, 300] 26 # 27 # def scale(width, height) 28 # # do something 29 # end 30 31 # Create different versions of your uploaded files: 32 # version :thumb do 33 # process resize_to_fit: [50, 50] 34 # end 35 36 # Add a white list of extensions which are allowed to be uploaded. 37 # For images you might use something like this: 38 # def extension_whitelist 39 # %w(jpg jpeg gif png) 40 # end 41 42 # Override the filename of the uploaded files: 43 # Avoid using model.id or version_name here, see uploader/store.rb for details. 44 # def filename 45 # "something.jpg" if original_filename 46 # end 47end
ご有識のある方、ご助力お願いします。
蛇足な記述等あればそちらもご指摘いただけると幸いです。
あなたの回答
tips
プレビュー