小编rtd*_*tdp的帖子

是否在Gemfile中强制指定Gem版本用于rails应用程序

我想知道我应该为我添加到Gemfile的每个gem指定gem版本.早些时候我的项目很少,我没有为所有宝石指定任何版本,捆绑商负责处理它,这也很好.

但最近我开始研究过去6个月正在开发的项目.在该项目中,许多gem版本仅在Gemfile中指定,并且Gemfile.lock被忽略.最终解决版本冲突和升级少数宝石引起了很大的麻烦.

还要知道从应用程序版本控制中删除Gemfile.lock是不好的做法 - yehuda的好文章 - http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and -gemfile /

所以,我的问题是我应该为我在Gemfile中指定的每个gem指定版本,还是只指定name和bundler将处理版本?处理这个问题的最佳做法是什么?

更新 -

更新此问题以正确指定问题,因为Gemfile可以解决许多问题:)

问题1 - 每个开发人员都应该拥有相同版本的宝石.实际上将Gemfile.lock添加到版本控制中解决了这个问题.开发人员只需要注意他们运行'bundle install'/'bundle'而不是'bundle update',因为这也会更新版本.

问题2 - 一些宝石版本,如果改变,刹车应用程序代码.实际上,对于omniauth,存在这种类型的问题,因为API从一个版本更改为另一个版本.是的,为了保持应用程序的正常运行,需要为这个宝石指定版本.

我的问题. - 所以,在我的gemfile中,由于严格指定了A和B的版本,并且因为它们都依赖于不同版本的Z,这是依赖性,我甚至无法运行bundle install或bundle update.唯一的解决方案是删除版本并让bundler接听电话.这就是我有问题的原因 - Gemfile.version_specification_mandatory?#=> true/false

rubygems ruby-on-rails bundler

10
推荐指数
2
解决办法
1926
查看次数

Rails - Mongoid:模型中新添加的字段为旧数据对象提供 nil

我的模型如下:

class User
  include Mongoid::Document
  field :name
end
Run Code Online (Sandbox Code Playgroud)

将一些用户对象保存到数据库后,我添加了更多字段:

class User
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  field :name
  field :birthdate
end
Run Code Online (Sandbox Code Playgroud)

现在,我希望可以使用以下代码片段:

@user = User.all
@user.each do |u|
   puts u.name
   puts u.birthdate.strftime(#someFormat)
   puts u.created_at.strftime(#someFormat)
end
Run Code Online (Sandbox Code Playgroud)

不幸的是,因为我的旧user对象没有birthdate密钥,所以我收到此错误:strftime called on nil class

问题:

  • 我如何使用 Mongoid 处理此类情况?在 MySQL 中,当添加列时,它也会添加到旧行中。但如果我在 MongoDB 中看到,它不会添加新字段作为旧数据的键。
  • 该字段也存在此问题,created_at因为旧数据也没有该字段。

我正在寻找解决此问题的好方法,nil每次检查条件不是一个可扩展的选项,因为字段的数量将继续增加。

ruby-on-rails mongodb mongoid ruby-on-rails-3

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

Rails Number to Indian货币格式转换助手

我的号码像100000

当我用户number_to_currency(100000)

它给 - 100,000.00

但我怎么能得到像1,00,000.00这样的格式

因为100,000美元看起来不错,但要用Rs显示它我需要像这样的分隔符(',') - 1,00,00,000.00

ruby-on-rails view

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

在lib目录下的类中使用"params"和"cookies"方法 - rails 3

我在lib文件夹下创建了一个类来处理一些地理位置逻辑.此类是从控制器方法启动的.我希望这个类能够处理有关地理位置的所有问题,其中包括 - 查询外部api以从ip查找国家/地区,如果找到国家/地区则设置Cookie(以便在用户的每个请求时不会调用外部api).说,

class GeoLocation
end
Run Code Online (Sandbox Code Playgroud)

现在,在这个类中,我想访问"params"方法来访问参数以及"cookies"方法来设置cookie并访问它们.

那么我应该在这个类中包含什么才能使这些方法起作用.我尝试过 -

class GeoLocation
    include ActionController
    include ActionController::Cookies
end
Run Code Online (Sandbox Code Playgroud)

但这会给出错误.

我也尝试过这个 -

class GeoLocation < ActionController::Base 
Run Code Online (Sandbox Code Playgroud)

但没用.那么,我应该怎么做,或者有更好的替代方法.

ruby-on-rails ruby-on-rails-3

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

Rspec to skip devise action in controller - sign_in_and_redirect

I have integrated rails application with omniauth and devise integration. In one the controller I have -

def create
     # some 
     # stuff 
     # here
   sign_in_and_redirect(:person, @person)
     # some 
     # stuff 
     # here
 end 
Run Code Online (Sandbox Code Playgroud)

as this action is from devise, i shouldn't be testing this action but only presence of it(correct me here if i am wrong.). Also, as I am mocking this person object, it doesn't have methods to pass origin sign_in_and_redirect action.

So, how can test this controller …

ruby tdd rspec ruby-on-rails devise

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