###前提・実現したいこと
Ruby on Rails で画像投稿機能を実装したいと考えております。
imageMagickをインストールし、RMagickとCarrierwaveもインストールしました。
全ての準備が整い、localhost3000で画像投稿を試みたのですが、うまくいきませんので何かアドバイスいただけると幸いです。
###発生している問題・エラーメッセージ
サーバーを立ち上げる際にはエラーは起きず、画像投稿をしようとすると
1 error prohibited this post from being saved: User must exist
###該当のソースコード
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 process :resize_to_limit => [100,100] # 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
posts/_form.html.erb
<%= form_for(post) do |f| %> <% if post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% post.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="fielad"> <%= f.label :image %> <%= f.file_field :image %> </div> <div class="field"> <%= f.label :title %> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :content %> <%= f.text_area :content %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
post.rb
class Post < ApplicationRecord belongs_to :User mount_uploader :image, ImageUploader end
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :posts end
###試したこと
色々調べたつもりなのですが、rubyのエラーではなくてページにてエラーが表示されるので見つからず困っております。どうかお力添えいただけたら幸いです。
###補足情報(言語/FW/ツール等のバージョンなど)
環境:mac OS Sierra ver.10.12.1
ruby 5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。