小编zol*_*ter的帖子

如何自定义活动的管理布局?

我需要自定义活动的管理员布局,但我该怎么做呢?

layout rubygems ruby-on-rails activeadmin

22
推荐指数
3
解决办法
2万
查看次数

比较float数组时rspec测试的舍入问题

我想检查一个结果的方法:

 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)

arrays testing floating-point rspec ruby-on-rails

12
推荐指数
1
解决办法
4983
查看次数

Rails,如何将数据从开发sqlite3数据库迁移到生产MySQL数据库?

使用Rails,如何将数据从开发sqlite3数据库迁移到生产MySQL数据库?

如何让它更容易?

mysql migration sqlite ruby-on-rails

9
推荐指数
1
解决办法
6697
查看次数

如何用Sunspot突出显示单词?

我想强调找到的单词文本,例如,如图所示这里.

据我所知,我必须遵循以下步骤:

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)

但它没有按预期工作,它总是突出显示标题的第一个单词,即使搜索到的单词位于其末尾.

有更简单的方法吗?

ruby search ruby-on-rails highlighting sunspot

5
推荐指数
1
解决办法
2474
查看次数

我可以使用什么样的wysiwyg编辑器来支持图像上传器的rails 3.1应用程序?

我尝试了这个编辑器,但收到了很多错误,可能存在一些编辑器,我可以轻松安装和更新图像.

我的回答:现在我使用这个编辑器,非常容易安装.

wysiwyg ruby-on-rails editor image-uploading

5
推荐指数
1
解决办法
3078
查看次数

Rails翻译自定义基本错误

我的模特:

产品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)

ruby-on-rails rails-i18n

5
推荐指数
1
解决办法
2578
查看次数

devise_invitable:邀请后确认

我重写了设计的确认!向我的用户发送欢迎消息的方法:

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)

ruby-on-rails actionmailer invite devise ruby-on-rails-3

4
推荐指数
2
解决办法
4386
查看次数

如何在Rails中使用CoffeeScript?

在我的控制器的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

3
推荐指数
1
解决办法
969
查看次数

使用限制和所有关联转储一个数据库表的最佳方法

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还是我应该怎么做?

mysql yaml dump ruby-on-rails mysqldump

3
推荐指数
1
解决办法
3539
查看次数

Rails 5 Capistrano资产

我正在使用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)

capistrano ruby-on-rails

3
推荐指数
1
解决办法
936
查看次数

Carrierwave俄语文件名

我需要保存带有俄语名称或音译文件名的文件.我试试这个:

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

0
推荐指数
1
解决办法
1440
查看次数