我正在向API发布日期,所需格式如下:
2014-12-01T01:29:18
Run Code Online (Sandbox Code Playgroud)
我可以像这样从模型中获取日期:
Model.created_at.to_s
Run Code Online (Sandbox Code Playgroud)
返回:
2014-12-01 01:29:18 -0500
Run Code Online (Sandbox Code Playgroud)
如何使用Rails或Ruby将其格式化为T所需的格式并删除-0500?
谢谢
我使用Rails 5和ActiveJob来处理后台任务.我试图将序列化的对象传递as_json给我的工作,但我收到以下错误:
ActiveJob::SerializationError (Unsupported argument type: Time):
ActiveJob::SerializationError (Unsupported argument type: DateTime):
Run Code Online (Sandbox Code Playgroud)
我知道ActiveJob不会采用Time/DateTime对象,因为某些排队系统不处理这些类型.所以我试图序列化的对象如下:
card = Card.first
=> #<Card id: 256, title: "quis", description: "Sunt corporis error laudantium veritatis impedit r...", due_date: "2016-12-15 12:00:00", slug: "quis", created_at: "2016-11-30 17:00:01", updated_at: "2016-11-30 17:00:01", list_id: 26, position: 0, period_type: "hours", period_length: 0.0, user_id: 1>
Run Code Online (Sandbox Code Playgroud)
当我跑:
card.as_json
=> {"id"=>256, "title"=>"quis", "description"=>"Sunt corporis error laudantium veritatis impedit repellat quasi.", "due_date"=>Wed, 15 Dec 2016 12:00:00 UTC +00:00, "slug"=>"quis", "created_at"=>Wed, 30 Nov 2016 17:00:01 UTC …Run Code Online (Sandbox Code Playgroud) 我有一个包含不到 5000 万行的表。它达到了 INT 的限制 (2147483647)。目前该表尚未被写入。
我计划将 ID 列从 INT 更改为 BIGINT。我正在使用 Rails 迁移通过以下迁移来执行此操作:
def up
execute('ALTER TABLE table_name MODIFY COLUMN id BIGINT(8) NOT NULL AUTO_INCREMENT')
end
Run Code Online (Sandbox Code Playgroud)
我已经在 2000 行的数据集上进行了本地测试,效果很好。在 5000 万条上运行ALTER TABLE命令应该没问题,因为目前该表没有被使用?
我想在运行迁移之前进行检查。任何意见将不胜感激,谢谢!
我在我的应用程序中使用rails_admin.我的模型上有一些范围,下面是一个例子:
class User < ActiveRecord::Base
scope :unconfirmed, where('confirmed_at IS NULL')
end
Run Code Online (Sandbox Code Playgroud)
在rails_admin中是否可以作为过滤器访问这些范围?就像你可以在主动管理员.就像在用户部分的某处添加按钮一样.
谢谢
我已经运行了Rails站点两年了,并且根据权重字段从数据库中提取了一些文章。数据结构为:
{name: 'Content Piece 1', weight: 50}
{name: 'Content Piece 2', weight: 25}
{name: 'Content Piece 3', weight: 25}
Run Code Online (Sandbox Code Playgroud)
我最初编写的Ruby代码如下所示:
choices = []
sum = articles.inject(0.0) { |sum, article|
sum += listing['weight']
}
pick = rand(sum)
choices << articles.detect { |listing|
if pick <= listing['weight']
true
else
pick -= listing['weight']
false
end
}
Run Code Online (Sandbox Code Playgroud)
这在拉出每个内容块并尊重重量方面效果很好。在整个数据集上运行此代码100次之后,根据权重,我多次获得了相当不错的内容:
100.times do
choices = []
sum = articles.inject(0.0) { |sum, article|
sum += listing['weight']
}
pick = rand(sum)
choices << articles.detect { |listing|
if pick <= …Run Code Online (Sandbox Code Playgroud) 我已经在我的台式机上安装了wkhtmltopdf 0.12.3(带有修补的qt)和我的Centos虚拟机上完全相同的版本.
我正在使用WickedPDF和Rails将HTML转换为PDF.
生成PDF时,它在字体上略有不同.一些奇怪的字母间距.我附上的图片显示了这一点.
你可以看到e和n之间的差距但是我的本地机器上没有.图片如下:
有谁知道为什么会这样?
任何意见,将不胜感激.
谢谢
这是我使用WickedPDF生成PDF的Ruby代码
def generate_pdf
pdf = WickedPdf.new
pdf_file = pdf.pdf_from_string(ActionController::Base.new().render_to_string('card_requests/show.pdf.haml', layout: 'pdf', locals: {card_request: self}),
dpi: '300',
margin: {top: 0, bottom: 0, left: 0, right: 0},
page_height: '2.25in',
page_width: '3.75in',
disable_smart_shrinking: false
)
tempfile = Tempfile.new(["#{self.name.gsub(' ', '_').downcase}_biz_card", '.pdf'], Rails.root.join('pdfs'))
tempfile.binmode
tempfile.write pdf_file
tempfile.close
self.pdf = File.open(tempfile.path)
tempfile.unlink
self.save
end
Run Code Online (Sandbox Code Playgroud)
这里也是show.pdf.haml文件,顶部有CSS:
!!! 5
%html
%head
:css
html * {
margin: 0 !important;
padding: 0 !important;
page-break-inside: avoid …Run Code Online (Sandbox Code Playgroud) 我已经开始在我的应用程序中使用Angular Material.它非常好用,并且易于实现,但我遇到了自定义调色板的问题.
我想知道是否有人主题为Angular Material的黑暗主题?
我正在考虑使用这个:
https://material.angularjs.org/#/Theming/03_configuring_a_theme
任何帮助,将不胜感激.
谢谢
ruby ×4
alter ×1
angularjs ×1
css ×1
date ×1
date-format ×1
datetime ×1
fonts ×1
mysql ×1
rails-admin ×1
random ×1
scope ×1
weighted ×1
wicked-pdf ×1
wkhtmltopdf ×1