小编Mat*_*ias的帖子

ruby模块和类在结构中的名称相同

我的一个项目中有如下文件夹结构:

  • LIB
    • bar.rb
    • 酒吧
      • other_bar.rb
      • another_bar.rb
      • next_bar.rb
      • ...

bar.rb

require File.expand_path(File.dirname(__FILE__) + "/bar/other_bar.rb")

class Bar
  puts "running BarBase"
end
Run Code Online (Sandbox Code Playgroud)

酒吧/ other_bar.rb

module Bar
  class OtherBar
    puts "running module Bar with class OtherBar"
  end
end
Run Code Online (Sandbox Code Playgroud)

如果我现在跑,ruby bar.rb我得到这个:

运行模块Bar with class OtherBar
bar.rb:3:in`':Bar不是类(TypeError)

我想要一个类似于rails模型继承结构的结构.我怎样才能解决这个问题?据我所知,ruby不支持开箱即用.这种情况有解决方法吗?

ruby ruby-on-rails

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

heroku上的bitbucket私有存储库

我有一个需要宝石的rails应用程序.我在bitbucket上将这个gem托管在私有存储库中.

在我的Gemfile中,我添加了如下的gem:

gem "my-gem", :git => "git@bitbucket.org:my-username/my-gem.git", :branch => 'master'
Run Code Online (Sandbox Code Playgroud)

我想在heroku上部署我的rails应用程序

git push heroku master
Run Code Online (Sandbox Code Playgroud)

现在我总是得到以下错误

Fetching git@bitbucket.org:my-username/my-git-repo.git
Host key verification failed.
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)

我理解错误,因为存储库设置为私有.但是我该如何解决这个问题呢?

我已经读过这个问题:在bitbucket上使用git部署到Heroku,但我没有真正得到答案:) ..

ruby-on-rails heroku bitbucket

13
推荐指数
3
解决办法
6728
查看次数

需要依赖才能获得Rails子类

我有以下设置:

应用程序/模型/ my_module/service.rb

module MyModule
  class Service < ActiveRecord::Base
    def self.types
      self.subclasses
    end

    def self.raw_types
      self.types.map { |c| c.name.split("::").last }
    end
  end
end

require_dependency "my_module/service/rack"
require_dependency "my_module/service/rails"
require_dependency "my_module/service/sinatra"
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ my_module /服务/ rack.rb:

module MyModule
  class Service::Rack < Service
  end
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ my_module /服务/ rails.rb:

module MyModule
  class Service::Rails < Service
  end
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ my_module /服务/ sinatra.rb:

module MyModule
  class Service::Sinatra < Service
  end
end
Run Code Online (Sandbox Code Playgroud)

这到目前为止工作,但现在我的问题:

为什么我要添加这三行:

require_dependency "my_module/service/rack"
require_dependency "my_module/service/rails"
require_dependency "my_module/service/sinatra"
Run Code Online (Sandbox Code Playgroud)

到我的service.rb文件?

如果我不添加三行:

MyModule::Service.raw_types
=> []
Run Code Online (Sandbox Code Playgroud)

如果我添加三行:

MyModule::Service.raw_types …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-4

8
推荐指数
1
解决办法
5172
查看次数

SimpleCov:不是每次都运行,只是使用rake任务

是否有可能在rake任务上运行simplecov coverage-tool而不是每次运行测试时?

gem ruby-on-rails

7
推荐指数
1
解决办法
1922
查看次数

设计注册确认

我的项目中有User和Admin角色.我用Devise创建了我的身份验证.

在我的管理员角色中,我没有任何确认.在我的用户模型中,我有以下内容:

devise :database_authenticatable, :confirmable, :recoverable,
       :rememberable, :trackable, :validatable, :timeoutable, :registerable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username, :prename, :surname, :phone, :street, :number, :location,
                :password, :password_confirmation
Run Code Online (Sandbox Code Playgroud)

我的迁移看起来像:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.confirmable
      t.recoverable
      t.rememberable
      t.trackable
      t.timeoutable
      t.validateable
      t.string  :username
      t.string  :prename
      t.string  :surname
      t.string  :phone
      t.string  :street
      t.integer :number
      t.string  :location

      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :confirmation_token,   :unique => true
    add_index …
Run Code Online (Sandbox Code Playgroud)

ruby rubygems ruby-on-rails

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

capybara poltergeist - 覆盖用户代理

有人知道如何将capybara poltergeist的用户代理覆盖到移动的用户代理进行测试吗?

我发现了为selenium webdriver配置的一些内容:http: //blog.plataformatec.com.br/2011/03/configuring-user-agents-with-capybara-selenium-webdriver/

如何在水豚顽固分子中做到这一点?

ruby gem rubygems ruby-on-rails ruby-on-rails-3

5
推荐指数
2
解决办法
5178
查看次数

Sidekiq + Devise Mailer有多个子域名

我在我的一个项目中在运行时更改ActionMailer :: Base.default_url_options = {:host => host}时遇到问题.

设置:我有多个子域,它们在后端使用相同的Rails-Application.我想通过Sidekiq队列将我的Devise邮件发送给用户.设计邮件(确认,重置密码)包含链接,这些链接需要特定的子域才是正确的.

我的环境

rails (4.2.0)
sidekiq (3.3.1)
devise (3.4.1)
devise-async (0.9.0)
Run Code Online (Sandbox Code Playgroud)

我的application_controller中有一个before_action

class ApplicationController < ActionController::Base

  before_action :set_action_mailer_default_url_options

  private

  def set_action_mailer_default_url_options
    host = "my-logic-to-get-the-correct-host"
    ActionMailer::Base.default_url_options = {:host => host}
  end

end
Run Code Online (Sandbox Code Playgroud)

现在,如果我想重置密码,我总是会得到我在环境文件中指定的默认网址选项.如果我从我的环境中删除default_url_options,我会在sidekiq日志中获得默认的Devise错误.

ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Run Code Online (Sandbox Code Playgroud)

主机始终在控制器中正确设置.我已经调试过了.似乎主机没有传递给sidekiq ..任何想法如何解决这个问题?

我无法在某个地方保存子域名,因为用户可以触发来自不同子域的电子邮件,但并不总是相同.我需要一种方法告诉Devise应该使用哪个主机发送特定的电子邮件.我可以覆盖Devise邮件程序并传递主机或类似的东西吗?

在我的情况下,以下解决方案是不可能的: 使用sidekiq时Rails动作邮件程序中的动态域名

顺便说一句:完整的工作流程,包括设计,设计 - 异步和sidekiq本身(在开发,分期和生产中).只有链接的主机不正确 - >这是我的大问题:-) ..

devise sidekiq ruby-on-rails-4 devise-async

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

在Ruby中使用Split时保留变音字符

为什么这段代码(包含变音符号):

text = "Some super text with a german umlaut Wirtschaftsprüfer"
words = text.split(/\W+/)
words.each do |w|
  puts w
end
Run Code Online (Sandbox Code Playgroud)

返回此结果(不保留以前给定的变音符号):

=> Some
=> super
=> text
=> with
=> a
=> german
=> umlaut
=> Wirtschaftspr
=> fer
Run Code Online (Sandbox Code Playgroud)

在Ruby 1.9+中分割字符串时,有没有办法保留变音符号?

编辑:我使用ruby 1.9.3p286(2012-10-12修订版37165)[x86_64-darwin11.4.2]

ruby split

2
推荐指数
1
解决办法
354
查看次数

在其他rails应用程序中安装gem包

我有以下设置:

一个rails 4.0.0 application =>我的主应用程序

通过这个应用程序,开发人员可以创建gem骨架

现在我想创建gem骨架源代码并通过rails master应用程序中的调用运行gem Gemfile的bundle install:

class MyClass

  # this works
  def create_gem_skeleton
    path = "path-to-gem-skeleton-outside-the-rails-master-app"
    FileUtils.mkdir_p(path)
    `cd #{path} && bundle gem my-new-gem`
  end

  # this method gets called, after I created the gem skeleton and manipulated it a bit with my preferences
  def my_method
    path = "path-to-gem-skeleton-outside-the-rails-master-app"
    exec `cd #{path} && bundle install`   # does not work, installs always the rails master bundle inside my rails master application, never touches the new gem-skeleton
    system `cd …
Run Code Online (Sandbox Code Playgroud)

ruby rubygems ruby-on-rails bundler

2
推荐指数
1
解决办法
357
查看次数