小编Bvu*_*Ic7的帖子

Rails - 最佳实践:如何创建依赖的has_one关系

你能告诉我创建has_one关系的最佳做法吗?

如果我有一个用户模型,它必须有一个配置文件...

我怎么能做到这一点?

一个解决方案是:

# user.rb
class User << ActiveRecord::Base
  after_create :set_default_association

  def set_default_association
    self.create_profile
  end
end
Run Code Online (Sandbox Code Playgroud)

但这似乎不是很干净......有什么建议吗?

ruby activerecord ruby-on-rails associations ruby-on-rails-3

74
推荐指数
5
解决办法
4万
查看次数

例外通知Gem和Rails 3

我正在尝试启动并运行,但每当我启动服务器时,我都会看到"未初始化的常量ExceptionNotifier".

http://github.com/rails/exception_notification

在我的Gemfile中我有

gem"exception_notification",:git =>" http://github.com/rails/exception_notification.git " ,: branch =>"master"

我已经尝试将配置如config/application.rb,config/environment.rb和config.ru中的github readme所示.我用我的应用程序名称替换了"Whatever".

exception-handling ruby-on-rails ruby-on-rails-plugins ruby-on-rails-3

41
推荐指数
5
解决办法
3万
查看次数

观察员与回调

我想过使用观察者或回调.什么时候应该使用观察者?

你可以做以下事情:

# User-model
class User << AR
  after_create :send_greeting!

  def send_greeting!
    UserNotifier.deliver_greeting_message(self)
  end

end

#observer
class UserNotifier << AR
  def greeting_message(user)
  ...
  end
end
Run Code Online (Sandbox Code Playgroud)

或者您可以创建一个观察者并让它在用户创建时观察...

你推荐什么?

ruby-on-rails callback observer-pattern

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

禁用:rails3中的.format路由

你能告诉我如何在rails路由中禁用.:format选项吗?我只需要HTML ...

formatting routing ruby-on-rails ruby-on-rails-3

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

最好的Rails标记插件/宝石

你推荐用于标记的插件或宝石是什么?其中有很多,acts_as_taggable,acts_as_taggable_on_steroids,acts_as_taggable_on,...

你说什么?

ruby rubygems ruby-on-rails ruby-on-rails-plugins

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

轨道中常量哈希的国际化3

你能告诉我在rails3中存储国际化常量的最佳实践吗?

我想为我的用户模型使用haircolours的常量哈希值:

# btw: how can I store such hashes in the locales.yml-files?
# en.yml
HAIR_COLOURS = { "brown" => 0, "white" => 1, "red" => 2, "dark-brown" => 3...}

# de.yml
HAIR_COLOURS = { "braun" => 0, "weiss" => 1, "rot" => 2, "dunkel-braun" => 3...}

# i18n.default_locale = :de
User.find(1).haircolour
=> 0
User.find(1).haircolour_str
=> "brown"

# i18n.default_locale = :de
User.find(1).haircolour
=> 0
User.find(1).haircolour_str
=> "braun"
Run Code Online (Sandbox Code Playgroud)

hash ruby-on-rails constants internationalization ruby-on-rails-3

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

Rspec,shoulda,validate_uniqueness_of,带有范围和错误的错误消息

我有以下Rspec测试:

describe Productlimit do

  before(:each) do 
    @productlimit = Factory.create(:productlimit, :user => Factory.create(:user))
  end

  subject { @productlimit }

  ...

  it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
  ...
end
Run Code Online (Sandbox Code Playgroud)

但我得到以下令人困惑的错误:

1) Productlimit 
     Failure/Error: it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
       Expected errors to include "has already been taken" when price_cents is set to 9530, got errors: ["direction_down has already been taken (false)"]
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗?我不明白为什么这不起作用,因为错误信息似乎是正确的?

编辑:

在其他情况下也会发生这种情况:

# product_spec.rb
...
it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") }  

# …
Run Code Online (Sandbox Code Playgroud)

validation rspec ruby-on-rails shoulda ruby-on-rails-3

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

最佳实践:我应该使用AR模型还是全局Hash来获取静态数据?

我正在考虑一个社交网站.我的用户模型应该有一个属性"eyecolor",可以在带有选择框/下拉列表的视图页面上设置.

我的问题: - >我应该制作AR模型还是应该使用全局哈希/常量来获取数据?是否有"静态模型"的最佳实践?

如何在没有AR模型的情况下关联以下关系:

u = User.first 
u.eyecolor 
==> 1 (not the eyecolor-string!)
Run Code Online (Sandbox Code Playgroud)

我需要的是eyecolor-string:

u = User.first 
u.eyecolor 
==> "brown"
Run Code Online (Sandbox Code Playgroud)

谢谢,抱歉我的英语不好!

ruby-on-rails

13
推荐指数
2
解决办法
1608
查看次数

save_and_open_page和spork,spork正在失去测试套件/输出

当我使用spork运行我的rspec测试时,每次使用capybara的save_and_open_page时,spork都会丢失测试套件..或者可能不再输出任何东西......

查看日志

# => without save_and_open_page
09:04:24 - INFO - Spork server for RSpec, Test::Unit successfully started

09:04:24 - INFO - Guard::RSpec is running
09:04:24 - INFO - Running all specs
Running tests with args ["--drb", "-f", "progress", "-r", "/Users/myuser/.rvm/gems/ruby-1.9.3-p392/gems/guard-rspec-2.5.2/lib/guard/rspec/formatter.rb", "-f", "Guard::RSpec::Formatter", "--failure-exit-code", "2", "spec"]...
................

Finished in 4.1 seconds
16 examples, 0 failures


Randomized with seed 50331

Done.

# => with save_and_open_page, no .... are shown anymore
09:04:29 - INFO - Guard is now watching at '/Users/myuser/coding/myproject'
09:04:39 - …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails capybara spork ruby-on-rails-3

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

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