【问题标题】:Image not displayed using Paperclip in Rails在 Rails 中使用 Paperclip 不显示图像
【发布时间】:2015-02-25 11:11:54
【问题描述】:

您好,我是 ruby​​ on rails 的初学者,我正在制作应用程序,我们可以在其中上传然后显示用户的图像,但图像没有上传和显示。 这是我的代码:

我的上传页面代码是:

  <%= form_for @userinfo , :html => { :multipart => true }  do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control'  %>
  <%= f.file_field :photo , class: 'form-control' %>

  <%= f.submit "Save Details", class: "btn btn-primary" %>
  <% end %> 

我的显示页面代码是:

  <%= image_tag @userinfo.photo.url %>
  <p>
     <strong>Name:</strong>
     <%= @userinfo.name %>
  </p>

我的型号代码是:

  class Userinfo < ActiveRecord::Base
    has_attached_file :photo
    validates_attachment_presence :photo
    validates_attachment_size :photo, :less_than => 5.megabytes
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  end

我的控制器代码是:

  class UserinfoController < ApplicationController
    def new
      @userinfo= Userinfo.new
    end
    def create
      @userinfo = Userinfo.new(user_params)    
      if @userinfo.save
        flash[:success] = "User created Successfully!!"
        redirect_to userinfo_path
      else
        render 'new'
      end
    end
    def show
       @userinfo = Userinfo.find(params[:id])
    end

    private
    def user_params
        params.require(:userinfo).permit(:name, :father_name, :mother_name, :contact_number, :photo)
    end
  end 

我从日志文件中得到的错误是:“ActionController::RoutingError (No route matches [GET] "/photos/original/missing.png")”并且在显示页面上它显示“missing”而不是显示图片。 我是初学者,所以告诉我我做错了什么。谢谢。

【问题讨论】:

  • 哪个页面出错了?如果是显示页面,那么您是否也可以包含来自控制器的显示操作代码?
  • 我们是否必须将has_attached_file 属性传递给params.require(:userinfo).permit
  • 你没有在模型中指定:url和:path?
  • @MaxWilliams:我在显示页面上收到错误消息。它不显示图像,而是显示“缺少”他们的。
  • @Deema:我必须指定哪个 url 和路径以及如何指定?能详细点吗?

标签: ruby-on-rails paperclip


【解决方案1】:

不是这样的答案,而是调试指南。

  • 查看日志以了解params[:id] 中的显示操作发生了什么
  • 在您的控制台中,使用该 ID 加载 Userinfo,例如 userinfo = Userinfo.find(123)
  • 在控制台中,执行userinfo.photouserinfo.photo.url - 这些看起来对吗?
    • 如果它们看起来没问题,请检查文件是否在它们指向的位置。文件路径与您的公用文件夹相关。
    • 如果它们看起来不正确,那么问题可能是它们一开始就没有正确上传。检查您的上传过程是否正常工作,并正确设置与 Userinfo 对象的图像关联。

【讨论】:

    【解决方案2】:

    我认为你会定义附件的 default_url,例如

    :default_url => "/images/:style/missing.png"
    

    您需要将其删除并使用有效率为 99.99% 的基本定义。

    attr_accessible :avatar
    has_attached_file :avatar,
    :url  => "/assets/images/users/:id/:style/:basename.:extension",
    ##user defined path
    :path => :rails_root/public/assets/images/:id/:style/:basename.:extension",
    :styles => { :medium => "300x300>", :thumb => "100x100>" },
    ##commenting the datault url
    ##:default_url => "/images/:style/missing.png"
    

    编辑答案:-

    So for a model user_info having avatar ..the view part with form_f

    form_for(@user_info,url:your_post_path) do |f|
    ## use html instead
      <input type="file" name="user_info[avatar][]" multiple>
    ##or use helper instead
      <%= f.file_field :avatar %>
    <%end%>
    

    【讨论】:

    • 当我这样做时,它给出了错误“#<0x007f75e8a906e0>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    相关资源
    最近更新 更多