只有“/images/original/missing.png”的回形针

时间:2014-06-17 19:45:35

标签: ruby-on-rails ruby imagemagick paperclip

我无法将上传的图片显示给用户。以下是我尝试过的事情:

  1. rails generate scaffold用户名:string age:integer
  2. rails生成回形针用户头像
  3. Gemfile中的相关宝石:

    gem "paperclip", "= 3.0.4"
    gem "cocaine", "= 0.3.2"
    

    型号:

    class User < ActiveRecord::Base
      has_attached_file :avatar, :styles => {:medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
      validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
    end
    

    查看表单(新功能和编辑):

    <%= form_for(@user , :html => {:multipart => true}) do |f| %>
      <% if @user.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
    
          <ul>
          <% @user.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
    
      <div class="field">
        <%= f.label :name %><br>
        <%= f.text_field :name %>
      </div>
      <div class="field">
        <%= f.label :age %><br>
        <%= f.number_field :age %>
      </div>
      <div class="field">
        <%= f.label :avatar %><br>
        <%= f.file_field :avatar%>
      </div>
    
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    

    查看(显示):

    <p id="notice"><%= notice %></p>
    
    <p>
      <strong>Name:</strong>
      <%= @user.name %>
    </p>
    
    <p>
      <strong>Age:</strong>
      <%= @user.age %>
    </p>
    
    <p>
      <strong>Avatar:</strong>
      <%= image_tag @user.avatar.url %>
    </p>
    
    <%= link_to 'Edit', edit_user_path(@user) %> |
    <%= link_to 'Back', users_path %>
    

    仍然只获得:/images/original/missing.png

1 个答案:

答案 0 :(得分:0)

你有一个UsersController还是你在使用Devise?如果是这样,请在下面查看我的代码。

 private

  # Use strong_parameters for attribute whitelisting
  # Be sure to update your create() and update() controller methods.

  def user_params
    params.require(:user).permit(:avatar)
  end