前提・実現したいこと
Ruby on Railsで自分の欲しいものを記録するインスタグラムのようなアプリを作っています。
gemのcarriewaveとminimagicを導入し、実装中なのですが上手く保存が出来ずundefined method `resize_to_limit' forというエラーが発生してしまいました。
このエラーを改善したいです。
発生している問題・エラーメッセージ
NoMethodError in PostsController#create undefined method `resize_to_limit' for #<ImageUploader:0x00007fe07ed77d08> Extracted source (around line #8): def create Post.create(post_params) end private Application Trace | Framework Trace | Full Trace app/controllers/posts_controller.rb:8:in `create' Request Parameters: {"authenticity_token"=>"2OXVrHt1xUU0t3BGMWRRdwj2cFu0nUa37mmEMSCJB7JA7pe3I8h/97UiprBi5B1p9HIY4HOO2SsdEKOEbDV6AA==", "post"=> {"img"=> #<ActionDispatch::Http::UploadedFile:0x00007fe07ed5f488 @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[img]\"; filename=\"IMG_6632.jpeg\"\r\n" + "Content-Type: image/jpeg\r\n", @original_filename="IMG_6632.jpeg", @tempfile=#<File:/var/folders/8h/t40pc8b131bbmlmsmnk6w2_r0000gn/T/RackMultipart20201208-8860-1via5s0.jpeg>>}, "commit"=>"アップロードする"}
20201208031516_add_img_to_posts.rb
class AddImgToPosts < ActiveRecord::Migration[6.0] def change add_column :posts, :img, :string end end
routes.rb
Rails.application.routes.draw do get 'items/download/:id', to: "items#download", as: "download_img" resources :posts devise_for :users root to: 'items#index' resources :items do resources :comments, only: :create collection do get 'search' end end resources :users, only: :show end
_form.html.haml
= form_with(model: item, local: true) do |form| = form.text_field :image, placeholder: "Image Url" = form.text_area :memo, pleaceholder: "memo", rows: "10" = form.submit "SEND" = form_for @post do |f| = f.file_field :img = f.submit 'アップロードする'
image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: storage :file # storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url(*args) # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: # process scale: [200, 300] # # def scale(width, height) # # do something # end # Create different versions of your uploaded files: # version :thumb do process resize_to_limit: [700, 700] # end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_whitelist %w(jpg jpeg gif png) end # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end end
post.rb
class Post < ApplicationRecord mount_uploader :img, ImageUploader end
item.rb
class Item < ApplicationRecord validates :memo, presence: true belongs_to :user has_many :comments def self.search(search) if search != "" Item.where('memo LIKE(?)', "%#{search}%") else Item.all end end end
posts_controller.rb
class PostsController < ApplicationController def new @post = Post.new(post_params) end def create Post.create(post_params) end private def post_params params.require(:post).permit(:img) end end
items_controller.rb
class ItemsController < ApplicationController before_action :set_item, only: [:edit, :show] before_action :move_to_index, except: [:index, :show, :search] def index @items = Item.includes(:user).order("created_at DESC") end def new @item = Item.new @post = Post.new(post_params) end def create Item.create(item_params) end def destroy item = Item.find(params[:id]) item.destroy end def edit end def update item = Item.find(params[:id]) item.update(item_params) end def show @comment = Comment.new @comments = @item.comments.includes(:user) end def search @items = Item.search(params[:keyword]) end def download send_data(@post.img.read,:filename=>@post.img_identifier) end private def item_params params.require(:item).permit(:image, :memo).merge(user_id: current_user.id) end def set_item @item = Item.find(params[:id]) end def move_to_index unless user_signed_in? redirect_to action: :index end end def post_params params.permit(:img) end end
試したこと
-
gemfile.lockにてminimagickがインストールされているのか確認をしました。
-
publicには画像は保存されているのか確認。保存されていました。
-
process resize_to_limit: [700, 700]だけでなく、他の数字も試してみました。
-
def extension_whitelist
%w(jpg jpeg gif png)
end
の記述をしました。
- items_controllerのdef post_params
params.require(:post).permit(:img)
end
としていたのですがrequireの記述を削除しました。
補足情報(FW/ツールのバージョンなど)
ご助言頂けると幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。