我定义了两种方法模型image_url和thumb_url获得绝对url图像和拇指,并呼吁他们控制.to_json()方法.
当我查看Json回复时,它只显示
请image_url不要thumb_url指导我在这里做错了什么.
型号:
class Post < ActiveRecord::Base
include Rails.application.routes.url_helpers
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x", thumb: "100x100#" } # # means crop the image
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
def image_url
relative_path = image.url(:medium)
self.add_host_prefix relative_path
end
def thumb_url
relative_path = image.url(:thumb)
self.add_host_prefix relative_path
end
def add_host_prefix(url)
URI.join(root_url, url).to_s
end
end
Run Code Online (Sandbox Code Playgroud)
控制器:
class Api::ImagesController < ApplicationController
def index
@posts = …Run Code Online (Sandbox Code Playgroud) 我正在使用 gemnokogiri来废弃img标签src值。有时url不显示带扩展名的图像文件名。
所以我试图检测图像MIME类型如下:
MIME::Types.type_for("http://web.com/img/12457634").first.content_type # => "image/gif"
Run Code Online (Sandbox Code Playgroud)
但它显示错误:
undefined method `content_type' for nil:NilClass (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
有什么解决办法吗?
我Rails API只用Postgreqsl数据库设置了项目。
导轨版本: 6.0.0.rc1
Rails_Admin 版本: 2.0.0.beta
我收到以下错误
RailsAdmin::MainController#dashboard 中的 ActiveRecord::StatementInvalid
PG::UndefinedTable: ERROR: relation "action_text_rich_texts" does not exist LINE 8: WHERE a.attrelid = '"action_text_rich_texts"'::regclass ^
Run Code Online (Sandbox Code Playgroud)
PS:有人说 Rails_Admin 也适用于 API only 项目。源链接
更新:我也尝试过创建一个新的 Rails 项目而不是使用--API标志。但错误仍然存在。