我想检查一个结果的方法:
result.should == [1.0,2.0,3.0]
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
expected: [1.0, 2.0, 3.0]
got: [1.0, 2.0, 3.0] (using ==)
Run Code Online (Sandbox Code Playgroud)
我认为四舍五入的问题,但我不知道如何比较它们,例如偏差为0.1.
谢谢你,apneadiving.我写了自己的匹配器,如果它可以帮助某人:
RSpec::Matchers.define :be_closed_array do |expected, truth|
match do |actual|
same = 0
for i in 0..actual.length-1
same +=1 if actual[i].round(truth) == expected[i].round(truth)
end
same == actual.length
end
failure_message_for_should do |actual|
"expected that #{actual} would be close to #{expected}"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not be close to #{expected}"
end
description do
"be a close to #{expected}"
end
end
Run Code Online (Sandbox Code Playgroud) 使用Rails,如何将数据从开发sqlite3数据库迁移到生产MySQL数据库?
如何让它更容易?
我想强调找到的单词文本,例如,如图所示这里.
据我所知,我必须遵循以下步骤:
1)在我的模型中,我必须在:stored => true我想要突出显示的字段中添加选项:
searchable do
text :title, :stored => true
text :description
end
Run Code Online (Sandbox Code Playgroud)
2)在我的控制器中,我必须声明我想要突出显示的字段:
def search
@search = Article.search do
keywords params[:search] do
highlight :title
end
end
end
Run Code Online (Sandbox Code Playgroud)
3)在视图中我不知道该怎么做,我试过这个:
- @search.each_hit_with_result do |hit, result|
%p= link_to raw(hit_title(hit)), article_path(result)
Run Code Online (Sandbox Code Playgroud)
这是做什么方法hit_title:
def hit_title(hit)
if highlight = hit.highlight(:title)
highlight.format { |word| "<font color='green'>#{word}</font>" }
else
h(hit.result.title)
end
end
Run Code Online (Sandbox Code Playgroud)
但它没有按预期工作,它总是突出显示标题的第一个单词,即使搜索到的单词位于其末尾.
有更简单的方法吗?
我的模特:
产品has_many型号has_many尺寸
对于尺寸我添加自定义错误,如下所示:
errors.add :base, "My custom error msg"
Run Code Online (Sandbox Code Playgroud)
在视图中我看到:"大小基础我的自定义错误消息"
class Size < ActiveRecord::Base
...
validate :custom_error, only: :update
...
def custom_error
errors.add :base, "My custom error msg"
end
end
Run Code Online (Sandbox Code Playgroud)
但是如何翻译这条消息呢?
更新我找到解决方案:
在locale.yml中:
attributes:
variants/sizes:
base: ''
Run Code Online (Sandbox Code Playgroud) 我重写了设计的确认!向我的用户发送欢迎消息的方法:
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
# ...
# Devise confirm! method overriden
def confirm!
UserMailer.welcome_alert(self).deliver
super
end
end
Run Code Online (Sandbox Code Playgroud)
用devise_invitable当用户接受邀请并设置密码时确认!方法永远不会被触发,是否有可能强迫它?devise_invitable如何确认用户?
或者也许我可以用同样的方式覆盖accept_invite(或其所谓的)方法?
我希望受邀用户保持未经证实,然后在接受邀请后确认.
谢谢,任何帮助非常感谢!
UPDATE
通过devise_invitable模型,我找到了可能导致这种不当行为的两种方法:
# Accept an invitation by clearing invitation token and confirming it if model
# is confirmable
def accept_invitation!
if self.invited? && self.valid?
self.invitation_token = nil
self.save
end
end
# Reset invitation token and send invitation again …Run Code Online (Sandbox Code Playgroud) 在我的控制器的create动作中,我有以下内容:
def create
@article = Article.find(params[:id])
respond_to do |format|
if @comment.save
format.js { render 'success.js' }
else
format.js { render 'failed.js' }
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我app/views/comments/failed.js.coffee,我有:
alert 'Write smth!' if $("#comments_error").length == 0
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ActionView::MissingTemplate (Missing template comments/failed,
inherited_resources/base/failed, application/failed with
{:locale=>[:en, :en],
:formats=>[:js, :html],
:handlers=>[:haml, :builder, :erb]})
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
javascript templates ruby-on-rails controller-action coffeescript
class Profile < ActiveRecord::Base
has_many :favorites, :dependent => :destroy
has_many :friends, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
我需要像这样的smth:
mysqldump --opt --where="1 limit 1000" -uroot development profiles > profiles.sql
Run Code Online (Sandbox Code Playgroud)
但是这个转储包含(按预期)只有1000个配置文件行,没有关联的朋友,收藏夹.
我应该使用YAML还是我应该怎么做?
我正在使用Rails 5 API Only应用程序。当我尝试部署此应用程序时出现错误
Tasks: TOP => deploy:assets:precompile
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as deploy@xxx.xxx.xxx: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
Don't know how to build task 'assets:precompile' (see --tasks)
/home/deploy/applications/myrento/shared/bundle/ruby/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/home/deploy/.rbenv/versions/2.3.1/bin/bundle:23:in `load'
/home/deploy/.rbenv/versions/2.3.1/bin/bundle:23:in `<main>'
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)
如何关闭deploy:assets:precompile任务?
这是我的Capfile
require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/rbenv"
require "capistrano"
require "capistrano/rails"
require "capistrano/bundler"
require …Run Code Online (Sandbox Code Playgroud) 我需要保存带有俄语名称或音译文件名的文件.我试试这个:
1)在document_uploader中:
def filename
@name ||= "#{Russian::transliterate(original_filename)}.#{file.extension}" if original_filename.present?
end
Run Code Online (Sandbox Code Playgroud)
2)在document.rb中:
我使用相同的逻辑创建before_create回调方法,但没有看到任何结果.
上传后的文件名称如下:"______________.doc"
我该做什么?
file-upload ruby-on-rails utf-8 character-encoding carrierwave
ruby-on-rails ×11
mysql ×2
actionmailer ×1
activeadmin ×1
arrays ×1
capistrano ×1
carrierwave ×1
coffeescript ×1
devise ×1
dump ×1
editor ×1
file-upload ×1
highlighting ×1
invite ×1
javascript ×1
layout ×1
migration ×1
mysqldump ×1
rails-i18n ×1
rspec ×1
ruby ×1
rubygems ×1
search ×1
sqlite ×1
sunspot ×1
templates ×1
testing ×1
utf-8 ×1
wysiwyg ×1
yaml ×1