###解決したいこと:Railsで 投稿した画像がDBに保存できるようにしたいです。
###初めて質問します
初心者です。
railsでアプリケーションを作成しています。
記事を投稿する時に画像も一緒に投稿したいのですが、テキストはDBに保存されるものの、画像が保存されません。
ネットで検索してみたところ、paperclipというものがあるみたいですね。
それをGemでインストールして、imagemagicも入れたのですが、うまくいきません。
ユーザー画面のavatarはうまく保存できているのですが...
どこがダメでどうしたらいいのでしょうか。
ユーザーの方のavatarは public/system/users/avatars/000/000/ のなかにそれぞれuser_idがで振り分けられて画像が保存されていることが確認できるのですが、投稿記事(posting)に関するそういったディレクトリが見当たりません。
これが理由でしょうか?
もしそうだとすれば、どうすれば画像をDBに保存できるようになりますか?
ご教授願います。
よろしくお願いいたします。
###ソースコード
controller class PostingsController < TeaInfoController before_action :authenticate_user!, only: :new def create @posting = Posting.new(user_id: current_user.id, image: posting_params[:image]) if @posting.save render :file => "/postings/create.html.erb" else render :new end end private def posting_params params.permit(:user_id, :image) end end model class Posting < ActiveRecord::Base belongs_to :user has_many :reviews validates :category_id, presence: true validates :name, presence: true has_attached_file :image, :styles => {medium:"500x500#", thumb:"300x300#"} validates_attachment_content_type :image, content_type: ["image/jpg", "image/jpeg", "image/png"] end migrate file class AddAttachmentImageToPostings < ActiveRecord::Migration def self.up change_table :postings do |t| t.attachment :image end end def self.down remove_attachment :postings, :image end end view <p>写真</p> <div class="posting_form"> <%= f.file_field :image %> </div><br/>
質問するのが初めてなので、足りない情報があれば教えてくださいませ
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/03 14:14