回形针宝石显示在ruby-on-rails上的破碎图像

时间:2014-12-26 18:45:53

标签: ruby-on-rails ruby-on-rails-4 paperclip paperclip-validation

我正在使用ruby on rails将照片上传到我的网站上使用回形针宝石,图片上传但显示它在网站上被破坏(即没有完全显示)我已经做了所有事情,甚至在线搜索解决方案但是我的网页上的图片显示为破碎。我不知道它有什么问题。我尝试过所有我知道的事情。任何帮助将不胜感激。

这是new form页面

的视图
<%= form_for @pic, :html => { :multipart => true } do |f| %>
  <% if @pic.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@pic.errors.count, "error") %> prohibited this pic from being saved:</h2>

      <ul>
      <% @pic.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>

    <div>
      <%= f.file_field :photo %>
    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是我的pic controller

class PicsController < ApplicationController
  before_action :set_pic, only: [:show, :edit, :update, :destroy]

  # GET /pics
  # GET /pics.json
  def index
    @pics = Pic.all
  end

  # GET /pics/1
  # GET /pics/1.json
  def show
  end

  # GET /pics/new
  def new
    @pic = Pic.new
  end

  # GET /pics/1/edit
  def edit
  end

  # POST /pics
  # POST /pics.json
  def create
    @pic = Pic.new(pic_params)

    respond_to do |format|
      if @pic.save
        format.html { redirect_to @pic, notice: 'Pic was successfully created.' }
        format.json { render :show, status: :created, location: @pic }
      else
        format.html { render :new }
        format.json { render json: @pic.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pics/1
  # PATCH/PUT /pics/1.json
  def update
    respond_to do |format|
      if @pic.update(pic_params)
        format.html { redirect_to @pic, notice: 'Pic was successfully updated.' }
        format.json { render :show, status: :ok, location: @pic }
      else
        format.html { render :edit }
        format.json { render json: @pic.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pics/1
  # DELETE /pics/1.json
  def destroy
    @pic.destroy
    respond_to do |format|
      format.html { redirect_to pics_url, notice: 'Pic was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pic
      @pic = Pic.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pic_params
      params.require(:pic).permit(:name, :description)
    end
end

这是view for my show page

<p id="notice"><%= notice %></p>

  <%= image_tag @pic.photo.url(:small) %>

<p>
  <strong>Name:</strong>
  <%= @pic.name %>
</p>

<p>
  <strong>Description:</strong>
  <%= @pic.description %>
</p>

<%= link_to 'Edit', edit_pic_path(@pic) %> |
<%= link_to 'Back', pics_path %>

这是我的Pics Model

class Pic < ActiveRecord::Base

  has_attached_file :photo,
                    styles: { medium: '300x300#', small: '100x100#', thumb: '25x25#' },
                    default_url: ->(attachment) { 'photo/:style.gif' },
                    convert_options: { all: '-set colorspace sRGB -strip' }

end

在我的路线中,我只有resources :pics

1 个答案:

答案 0 :(得分:2)

您未在可信参数中包含:photo。 试试这样:

params.require(:pic).permit(:name, :description, :photo)