【问题标题】:How can I get complete absolute URL for paperclip attachment?如何获得回形针附件的完整绝对 URL?
【发布时间】:2018-12-26 20:21:11
【问题描述】:

我正在处理 JSON 数据,我想显示带有 localhost 名称的完整图像 url 但我得到了一半 item_image_url..

"item_image_url": "/system/images/item_images/000/000/001/original/images_%284%29.jpeg?1545807832"

{
        "id": 1,
        "title": "Pasta",
        "filename": "Burger.",
        "item_image_file_name": "images_(4).jpeg",
        "item_image_content_type": "image/jpeg",
        "item_image_file_size": 12944,
        "item_image_updated_at": "2018-12-26T07:03:52.284Z",
        "updated_at": "2018-12-26T07:03:52.355Z",
        "created_at": "2018-12-26T07:03:52.355Z",
        "item_image_url": "/system/images/item_images/000/000/001/original/images_%284%29.jpeg?1545807832"
    }

我怎样才能像这样得到这个完整的 ulr http://localhost:3000/system/images/item_images/000/000/001/original/images_%284%29.jpeg?1545807832

在控制器中

class V1::ImagesController < ApplicationController

    skip_before_action :verify_authenticity_token

    def index

         @images = Image.all


         render :json => @images.to_json(:methods => [:item_image_url])
    end

    def show
        image = Image.find(params[:id])
        render json: {status: 'success', data:image},status: :ok
    end


    def create
         image = Image.new(image_params)
        if  image.save
            render json: {status: 'success', data:image},status: :ok    
        else
            render json: {status: 'error', message:'image not saved', data:image.errors},status: :unprocessable_entity  
        end 
    end


    def destroy
        image = Image.find(params[:id])
        image.destory
        render json: {status: 'success', data:image},status: :ok    
    end

    private def image_params

        params.permit(:item_image_url,:title,:filename)

    end

end

在模型中

class Image < ApplicationRecord

    has_attached_file :item_image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :item_image, content_type: /\Aimage\/.*\z/


    def item_image_url
        self.item_image.url(:original)
    end

end

请帮助..

【问题讨论】:

  • 我不需要任何帮助。

标签: ruby-on-rails ruby


【解决方案1】:

config/development.rb中的常量APP_URL定义为,

APP_URL = 'http://localhost:3000'

对于test.rb & production.rb,您必须根据您的域环境设置进行设置。

得到它,

def item_image_abs_url
    "#{APP_URL}#{item_image.url(:original)}"
end

检查您在 Rails 控制台中获得的以下网址,

image = Image.first
image.item_image.url

【讨论】:

  • item_image_abs_url 得到什么网址?
  • 你说我添加你的代码 config/development.rb = APP_URL = 'localhost:3000' 还有 test.rb 和 production.rb def item_image_abs_url "#{APP_URL}item_image.url(:original )}" 结束,但注意到工作状态相同,我在 json 数据上方显示..
  • 通过@image.item_image_url检查rails控制台并告诉你得到什么字符串
  • for @image.item_image_url = (undefined method item_image_url' for nil:NilClass)..for image.item_image_url = (undefined method item_image_url' for #<:activerecord_relation:0x000055665ee042b8>)
  • for image = Image.first =
【解决方案2】:

根据这个github issue,使用 ActionController::Base.asset_host 会更干净,所以它会产生助手:

  def add_host_prefix(url)
    URI.join(ActionController::Base.asset_host, url)
  end

这假设您在每个 /config/environments/environment.rb 文件中都有以下内容:

Appname::Application.configure do

  # ....

  config.action_controller.asset_host = 'http://localhost:3000' # Locally

  # ....

end

希望对你有帮助

【讨论】:

  • config.action_controller.asset_host = 'localhost:3000' 我将此代码添加到 /config/environments/.rb 但获取 = undefined local variable or method `config' for main:Object (NameError )
【解决方案3】:

根据帖子中提到的描述,您似乎没有在特定环境文件中指定资产主机。

上述答案中定义的适当方法也是在相关的 config/environments/{environment} 文件中定义资产主机: 其中上一行中提到的环境分别表示 development.rb、production.rb 和 test.rb(如果你定义了自定义环境,其他也一样)

onfig.action_controller.asset_host = "http://localhost:3000"

如果您想在视图中访问它,请使用下面提到的代码:

asset_url(model.attachment.url(:style))

如果你想访问 Rails 控制台,请使用下面提到的代码

helper.asset_url(model.attachment.url(:style))

如果您想在模型中使用它,请使用下面提到的代码:

ApplicationController.helpers.asset_url(model.attachment.url(:style))

希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多