0

私はレールを初めて使用し、YouTube https://www.youtube.com/watch?v=70Pu_28yvdIのチュートリアルに従っていて、約 40 分に達しました。新しい投稿を作成して画像を添付しようとすると、エラー画像の拡張子がその内容と一致しません。彼が行ったのとまったく同じコードをコーディングしましたが、エラーが発生し続けます。助けてくれてありがとう。

post.rb ファイル

class Post < ActiveRecord::Base
    belongs_to  :user
    has_attached_file :image, styles: { medium: "700x500#", small: "350x250" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

post_controller.rb ファイル

class PostsController < ApplicationController
    before_action :find_post, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!, except: [:index, :show]

    def index
        @post = Post.all.order("created_at DESC")   
    end

    def show
    end

    def new
        @post = current_user.posts.build
    end

    def create
        @post = current_user.posts.build(post_params)
        if @post.save
            redirect_to @post
        else
            render 'new'
        end 
    end

    def edit    
    end

    def update
        if @post.update(post_params)
            redirect_to @post
        else
            render 'edit'
        end 
    end

    def destroy
        @post.destroy
        redirect_to root_path   
    end

    private

    def find_post
        @post = Post.find(params[:id])
    end

    def post_params
        params.require(:post).permit(:title, :link, :description, :image)
    end

end

20150728130528_add_attachment_image_to_posts.rb ファイル

class AddAttachmentImageToPosts < ActiveRecord::Migration
  def self.up
    change_table :posts do |t|
      t.attachment :image
    end
  end

  def self.down
    remove_attachment :posts, :image
  end
end
4

1 に答える 1