使用回形针 rails 中缺少图像

Missing image in rails using paperclip

我是 rails 的新手,正在尝试构建我的第一个项目。 使用回形针添加图像时遇到问题。 基本上每当我尝试将图像上传到 post 时,它仍然保持 "missing" image.Any 的帮助非常感谢。 这是我的模型

 class Pt < ActiveRecord::Base
        validates :name, presence: true
        has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100#" }
        validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    end


  and my controller

class PersonaltrainersController < ApplicationController
  def index
      @PT = Pt.all
  end

  def new
      @PT = Pt.new
  end

  def show
      @PT = Pt.find(params[:id])
  end
def destroy
    @PT = Pt.find(params[:id])
  @PT.destroy

  redirect_to personaltrainers_path
end
def edit
      @PT = Pt.find(params[:id])
  end

    def update
        @PT = Pt.find(params[:id])
  if @PT.update(params[:pt].permit(:name, :age, :sex, :experience))
      redirect_to personaltrainer_path
        flash[:notice] = "The trainer was updated."
        else
            flash[:error] = "Something went wrong."
            render :edit
        end
            end

  def create
    @PT = Pt.new(params[:pt].permit(:name, :age, :sex, :experience))
      if @PT.save
            flash[:success] = "Successfully added a post."
            redirect_to personaltrainers_path
       else 
            flash[:error] = "Please check the form for errors and try again."
            render :new
        end
 end
      end
  def contact
  end

和我的展示页面

<h1> <%= @PT.name %></h1>
<h2> <%= @PT.age %></h2>
<body>
<%= @PT.experience %>
</body>
<br>
<%= image_tag @PT.image.url()%>
<%= link_to "edit", edit_personaltrainer_path %>
<%= link_to "delete", personaltrainer_path, method: :delete, confirm: true %>

问题出在您的许可参数上,这部分:

params[:pt].permit(:name, :age, :sex, :experience)

应包含 Pt 模型的 :image 属性。所以,你需要这个作为你的许可参数:

params[:pt].permit(:name, :age, :sex, :experience, :image)

这样应该可以正确保存您的图像。请记住经常检查您的服务器日志,当您忘记向您的许可参数添加属性时,它们会显示诸如 "Unpermitted parameters: :image" 之类的警告。