首页 文章

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

提问于
浏览
1

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

  • rails生成脚手架用户名:字符串年龄:整数

  • rails生成回形针用户头像

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

    你有用户控制器还是使用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
    

相关问题