Rails 回形针图像 URL 上传不起作用

发布于 2024-11-09 15:58:10 字数 4107 浏览 0 评论 0原文

我正在尝试创建一些图像网址上传。我的文件上传字段有效,但是当我提交 URL 时,它会提交并呈现我的索引页面,并且不会上传任何图像。

这是我的模型:

class Konkurrancer < ActiveRecord::Base

    has_attached_file :photo,
                      :url  => "/public/images/billeder/photo/:id/:basename.:extension",
                      :path => ":rails_root/public/images/billeder/photo/:id/:basename.:extension"

    has_attached_file :photo2,
                      :url  => "/public/images/billeder/photo2/:id/:basename.:extension",
                      :path => ":rails_root/public/images/billeder/photo2/:id/:basename.:extension"

      attr_accessor :photo_url, :photo_url2

      def photo_url=(url) 
        return if url.blank?
        self.photo = RemoteUpload.new(url)
      end

      def photo_url2=(url) 
        return if url.blank?
        self.photo2 = RemoteUpload.new(url)
      end
end

我的控制器:

    def new
        @konkurrancer = Konkurrancer.new
        if !params[:konkurrancer].nil? && !params[:konkurrancer][:photo_url].blank?
        # user entered a photo url, use the url
        @konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url]
        elsif !params[:konkurrancer].nil? && !params[:konkurrancer][:photo]
         # use file upload  
        @konkurrancer.photo = params[:konkurrancer][:photo]
    end

    if !params[:konkurrancer].nil? && !params[:konkurrancer][:photo_url_2].blank?
      # user entered a photo url, use the url
      @konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url_2]
    elsif !params[:konkurrancer].nil? && !params[:konkurrancer][:photo]
      # use file upload
      @konkurrancer.photo2 = params[:konkurrancer][:photo2]
    end

        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @konkurrancer }
        end
      end

  # POST /konkurrancers
      # POST /konkurrancers.xml
      def create
        @konkurrancer = Konkurrancer.new(params[:konkurrancer])

        respond_to do |format|
          if @konkurrancer.save
            format.html { redirect_to(:admin_konkurrancers, :notice => 'Konkurrancer was successfully created.') }
            format.xml  { render :xml => :admin_konkurrancers, :status => :created, :location => @konkurrancer }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @konkurrancer.errors, :status => :unprocessable_entity }
          end
        end
      end

我的remote_upload.rb:

require 'open-uri'

# Make it always write to tempfiles, never StringIO
OpenURI::Buffer.module_eval {
  remove_const :StringMax
  const_set :StringMax, 0
}

class RemoteUpload 

  attr_reader :original_filename, :attachment_data

  def initialize(url) 
    # read remote data
    @attachment_data    = open(url)

    # determine filename
    path = self.attachment_data.base_uri.path

    # we need this attribute for compatibility to paperclip etc.
    @original_filename = File.basename(path).downcase
  end

  # redirect method calls to uploaded file (like size etc.)
  def method_missing(symbol, *args)
    if self.attachment_data.respond_to? symbol
      self.attachment_data.send symbol, *args
    else
      super
    end
  end

end

我的视图:

<%= simple_form_for [:admin, @konkurrancer], :html => { :multipart => true } do |f| %>
    <%= f.input :name, :label => 'Titel', :style => 'width:500;' %>
    <%= f.label :upload_125x125 %>
    <%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %>
    <%= f.input :photo_url, :label => 'URL 125x125', :style => 'width:250;' %>
    <%= f.label :upload_460x60 %>
    <%= f.file_field :photo2, :label => '460x58', :style => 'width:250;' %>
    <%= f.input :photo_url2, :label => 'URL 460x58', :style => 'width:250;' %>
    <%= f.button :submit, :value => 'Create konkurrence' %>
<% end %>

I am trying to create some image url upload. My file upload field works, but when I submit a URL it submit and renders my indeks page and no image gets uploaded.

Here is my model:

class Konkurrancer < ActiveRecord::Base

    has_attached_file :photo,
                      :url  => "/public/images/billeder/photo/:id/:basename.:extension",
                      :path => ":rails_root/public/images/billeder/photo/:id/:basename.:extension"

    has_attached_file :photo2,
                      :url  => "/public/images/billeder/photo2/:id/:basename.:extension",
                      :path => ":rails_root/public/images/billeder/photo2/:id/:basename.:extension"

      attr_accessor :photo_url, :photo_url2

      def photo_url=(url) 
        return if url.blank?
        self.photo = RemoteUpload.new(url)
      end

      def photo_url2=(url) 
        return if url.blank?
        self.photo2 = RemoteUpload.new(url)
      end
end

My controller:

    def new
        @konkurrancer = Konkurrancer.new
        if !params[:konkurrancer].nil? && !params[:konkurrancer][:photo_url].blank?
        # user entered a photo url, use the url
        @konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url]
        elsif !params[:konkurrancer].nil? && !params[:konkurrancer][:photo]
         # use file upload  
        @konkurrancer.photo = params[:konkurrancer][:photo]
    end

    if !params[:konkurrancer].nil? && !params[:konkurrancer][:photo_url_2].blank?
      # user entered a photo url, use the url
      @konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url_2]
    elsif !params[:konkurrancer].nil? && !params[:konkurrancer][:photo]
      # use file upload
      @konkurrancer.photo2 = params[:konkurrancer][:photo2]
    end

        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @konkurrancer }
        end
      end

  # POST /konkurrancers
      # POST /konkurrancers.xml
      def create
        @konkurrancer = Konkurrancer.new(params[:konkurrancer])

        respond_to do |format|
          if @konkurrancer.save
            format.html { redirect_to(:admin_konkurrancers, :notice => 'Konkurrancer was successfully created.') }
            format.xml  { render :xml => :admin_konkurrancers, :status => :created, :location => @konkurrancer }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @konkurrancer.errors, :status => :unprocessable_entity }
          end
        end
      end

My remote_upload.rb:

require 'open-uri'

# Make it always write to tempfiles, never StringIO
OpenURI::Buffer.module_eval {
  remove_const :StringMax
  const_set :StringMax, 0
}

class RemoteUpload 

  attr_reader :original_filename, :attachment_data

  def initialize(url) 
    # read remote data
    @attachment_data    = open(url)

    # determine filename
    path = self.attachment_data.base_uri.path

    # we need this attribute for compatibility to paperclip etc.
    @original_filename = File.basename(path).downcase
  end

  # redirect method calls to uploaded file (like size etc.)
  def method_missing(symbol, *args)
    if self.attachment_data.respond_to? symbol
      self.attachment_data.send symbol, *args
    else
      super
    end
  end

end

My view:

<%= simple_form_for [:admin, @konkurrancer], :html => { :multipart => true } do |f| %>
    <%= f.input :name, :label => 'Titel', :style => 'width:500;' %>
    <%= f.label :upload_125x125 %>
    <%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %>
    <%= f.input :photo_url, :label => 'URL 125x125', :style => 'width:250;' %>
    <%= f.label :upload_460x60 %>
    <%= f.file_field :photo2, :label => '460x58', :style => 'width:250;' %>
    <%= f.input :photo_url2, :label => 'URL 460x58', :style => 'width:250;' %>
    <%= f.button :submit, :value => 'Create konkurrence' %>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-11-16 15:58:10

我正在写一篇漂亮的回复,然后我发现有人已经写了同样的东西!

在这里:http://trevorturk.com/ 2008/12/11/easy-upload-via-url-with-paperclip/

这应该正是您正在寻找的内容。如果你绊倒了,请告诉我。

祝你好运

I was in the middle of writing a pretty through response, and then I found someone already wrote the same thing!!

Here you go: http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

This should be exactly what you are looking for. If you stumble, let me know.

Good luck

时光沙漏 2024-11-16 15:58:10

您似乎正在尝试访问与表单中的参数不匹配的参数:

@konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url]


<%= f.input :photo_url, :label => 'URL 125x125', :style => 'width:250;' %>

您应该在控制器中使用 :photo_url 而不是 :photo_remote_url 吗?

It seems like you are trying to access a parameter that doesn't match what you have in the form:

@konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url]


<%= f.input :photo_url, :label => 'URL 125x125', :style => 'width:250;' %>

Should you be using :photo_url instead of :photo_remote_url in the controller?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文