Tag: ruby on rails

如何使用Rspec测试具有嵌套路由的控制器?

我有两个控制器,我使用铁轨脚手架发电机创建。 我希望它们嵌套在一个名为“demo”的文件夹中,然后运行 rails g scaffold demo/flows rails g scaffold demo/nodes 然后我决定在流内嵌套节点,并改变我的路由文件,如下所示: namespace :demo do resources :flows do resources :nodes end end 但是这种变化导致了对节点的rspec测试破坏了ActionController :: Routing错误。 15) Demo::NodesController DELETE destroy redirects to the demo_nodes list Failure/Error: delete :destroy, :id => “1” ActionController::RoutingError: No route matches {:id=>”1″, :controller=>”demo/nodes”, :action=>”destroy”} 问题是rspec正在寻找错误的路线。 它应该寻找“demo / flows / 1 / nodes”。 它还需要一个模拟流程模型,但我不知道如何提供。 […]

如何在Rails控制台中输入Unicode字符?

在使用Rails控制台时,当我输入ä ,会出现\U+FFC3\U+FFA4 。 当然我可以在rails之外输入Unicode字符。 我在Max OS X 10.7.5中使用Ruby 2.0.0p247,Rails 4.0.0。 如何在Rails控制台中输入Unicode字符?

Rails ActiveRecord:Three Table has_many through:associations

我正在尝试构建一个表来处理某个广告系列已使用以下模型关联设置的位置和类别: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Category < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids has_many :metros, through: :campaign_category_metro_bids end class CampaignCategoryMetroBid < ActiveRecord::Base belongs_to :campaign belongs_to […]

黄瓜测试 – 获取RoutingError

怎么了,伙计们。 请帮忙。 当我运行我的黄瓜测试时,我遇到了这个错误: No route matches {:action=>”show”, :controller=>”accounts”} (ActionController::RoutingError) ./features/support/paths.rb:40:in `path_to’ 耙路线显示: account GET /accounts/:id(.:format) {:action=>”show”, :controller=>”accounts”} cucumber_test.feature Scenario: Given… And… Then i should be on Show page function/支持/ paths.rb when /^Show page$/ account_path @account 的routes.rb Myapp::Application.routes.draw do resources :accounts

Rails是否提供默认的会话超时持续时间? 如果是,它在哪里指定?

我已经知道如何在rails app中设置到期时间,这在网上已经很好地记录了。 但我想知道的是在rails中会话到期的默认持续时间是什么,如果有,在哪里可以找到它?

Ruby roo LoadError:无法加载此类文件 – 电子表格/注释

尝试加载xls文件并获取此错误。 有控制台日志: irb(main):001:0> require ‘roo’ => true irb(main):002:0> a = Roo::Excel.new(‘/home/a.xls’) LoadError: cannot load such file — spreadsheet/note from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /var/lib/gems/1.9.1/gems/spreadsheet-0.9.2/lib/spreadsheet/excel/reader.rb:5:in `’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /var/lib/gems/1.9.1/gems/spreadsheet-0.9.2/lib/spreadsheet.rb:28:in `’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’ from /var/lib/gems/1.9.1/gems/roo-1.12.2/lib/roo/excel.rb:1:in `’ from (irb):2 from /usr/bin/irb:12:in `’ 请帮忙解决这个问题 此外,当我尝试需要电子表格时,我得到相同的错误 require ‘spreadsheet’ 但是这个模块是安装的 […]

Rails 2.3.2试图渲染ERB而不是HAML

Rails突然试图渲染ERB而不是Haml,我无法弄清楚原因。 我创建了新的rails项目,重新安装了Haml,并重新安装了Rails。 这正是我在制作应用程序时所采取的步骤(Rails 2.3.2): rails> rails test rails> cd test rails\test> haml –rails . rails\test> ruby script\generate model user email:string password:string rails\test> ruby script\generate controller users index rails\test> rake db:migrate 这是UsersController的样子: class UsersController < ApplicationController def index @users = User.all end end 我的路线: ActionController::Routing::Routes.draw do |map| map.resources :users end 我现在创建views \ users \ index.html.haml : […]

如何在Ruby on Rails中为自己的rake任务使用自定义日志?

我有一个rake任务调用这样的函数: namespace :blah do task :hello_world => :environment do logger.info(“Hello World”) helloworld2 end end def helloworld2 logger.info(“Hello Again, World”) end 我希望日志输出到自定义日志,我真的不希望每次进行函数调用时都要传递日志引用。 我发现这个地方(再也找不到了): def logger @@logger ||= Logger.new(“#{RAILS_HOME}/log/blah.log”) end 但是这对我不起作用,我不确定它甚至做了什么,因为我很久以前就抓住了代码并且直到现在才使用它。 我无法在google上搜索@@(尝试+“@@”rails)来查看它的作用。 对这个问题的任何帮助都会很棒。 我希望快速解决方案而不必安装gem或插件(除非有一个非常好的理由。 谢谢!

状态机,模型validation和RSpec

这是我当前的类定义和规范: class Event :not_started do event :game_started do transition :not_started => :in_progress end event :game_ended do transition :in_progress => :final end event :game_postponed do transition [:not_started, :in_progress] => :postponed end state :not_started, :in_progress, :postponed do validate :end_time_before_final end end def end_time_before_final return if end_time.blank? errors.add :end_time, “must be nil until event is final” if end_time.present? […]

Rails 3中的config.logger和config.paths.log

尝试启动服务器时出现此错误。 logger.rb:541:in `exist?’: can’t convert Rails::Paths::Path into String (TypeError) 这是我的development.rb中的代码。 require ‘log_formatter’ config.logger = Logger.new(config.paths.log.first) config.logger.formatter = LogFormatter.new config.logger.level = Logger::DEBUG 我试过添加.to_s,但是没用。 谷歌也没有帮助。