f.collection_select不显示所选值

这个搜索在谷歌上有这么多的结果,它甚至在SO上被问到 – 但到目前为止讨论的解决方案对我没有帮助。 这是问题:我有一个form_for @company |f| 我正在为company_status_id使用f.collection_select – 但是当表单加载时,如果设置了,我希望选择实际的公司状态。 通过我知道的调试器,它已被设置,但我在那里显示默认值。 这是代码: = puts @company.company_status_id = f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value} 这是生成的htmnl -Not Available- Active Bankrupt Acquired 即使我这样做,条件仍然相同: f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value, :selected => :selected => @company.company_status} 要么 f.collection_select :company_status_id, ListCache.company_statuses, :id, :name, {:prompt => @select_value, :selected => @company.company_status}

如何解决factory_girl错误的参数数量错误

#rspec test code @room = FactoryGirl.build(:room) #factory definition factory :room do length {10} width {20} end #code implementation class Room attr_accessor :length, :width def initialize(length,width) @length = length @width = width end end 尝试构建@room时,运行rspec会导致此错误 ArgumentError:参数数量错误(0表示2)

Rails 4嵌套在更新时属性多个记录

我被卡住了,我不知道为什么它不能正常工作。 我有一个有许多标签的模型产品。 当我更新产品rails时,正确更新产品属性,但是正在创建另一个标记记录,而不仅仅是更新它。 这是我的代码: 查看表格: 产品型号: class Product “user_id” has_many :tags, :dependent => :destroy accepts_nested_attributes_for :tags, reject_if: :all_blank, allow_destroy: true, :update_only => true end 标签型号: class Tag “product_id” # before_save { name.downcase! } end 产品控制器: def edit user = User.find(params[:user_id]) @product = user.products.find(params[:id]) @tags = @product.tags.all respond_to do |format| format.html format.js end end def update […]

如果不选择updated_at属性,则无法在Rails 4中更新模型

我正在从rails 3.2.13升级到4并遇到更新模型的问题。 我正在尝试更新我只有id的模型上的单个属性,我也想要validation和回调。 这在轨道3中运行良好,但我在轨道4中遇到了麻烦。 在轨道4中如果我这样做: user = User.select(:id).first user.first_name = ‘name’ user.save! 我收到错误: NoMethodError: undefined method `type_cast’ for nil:NilClass from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/attribute_methods/time_zone_conversion.rb:10:in `type_cast’ 如果我在rails 3中运行相同的代码,则更新first_name并使用current_time更新updated_at。 如果在轨道4中我运行: user = User.select(:id, :updated_at).first user.first_name = ‘name’ user.save! 一切正常。 我假设错误是由于updated_at列不存在而引起的,但它不应该是。 在我的许多查询中,我只选择id作为速度。 Rails 3完美地处理了这个问题。 我也尝试使用update_attributes但我收到了同样的错误。 当我使用update_column但它跳过我想要的validation和回调时,它可以正常工作。 我不确定为什么会在rails 4中发生这种情况,或者如何在没有显式调用update_column之后的validation和回调或者选择我不应该执行的updated_at属性的情况下解决这个问题。

Rails返回JSON序列化属性with_indifferent_access

我以前有: serialize :params, JSON 但是这将返回JSON并将哈希键符号转换为字符串。 我想使用符号引用哈希,这在使用哈希时最常见。 我给它添加符号,Rails返回字符串。 为了避免这种情况,我创建了自己的getter / setter。 setter很简单(JSON编码),getter是: def params read_attribute(:params) || JSON.parse(read_attribute(:params).to_json).with_indifferent_access end 我无法直接引用params ,因为这会导致循环,所以我使用read_attribute ,现在我的哈希键可以用符号或字符串引用。 但是,这不会更新哈希: model.params.merge!(test: ‘test’) puts model.params # => returns default params without merge 这让我觉得哈希是由副本引用的。 我的问题是双重的。 我是否可以扩展活动记录JSON序列化以返回无关紧要的访问哈希(或不将符号转换为字符串),并且仍然具有如上所述的哈希工作与合并? 如果没有,我该怎么做才能改善我的吸气剂,以便使用model.params.merge! 作品? 我希望有一些东西(有效): def params_merge!(hash) write_attribute(:params, read_attribute(:params).merge(hash)) end # usage: model.params_merge!(test: ‘test’) 更好的是,只需让Rails返回一个具有无差别访问权限的哈希值,或者不将我的符号转换为字符串! 感谢任何帮助。

如何使用Ruby模拟类?

我正在使用minitest / mock并且想要模拟一个类。 我不是试图测试模型类本身,而是试图测试服务(SomeService)与模型(SomeModel)的交互。 我想出了这个(Hack :: ClassDelegate),但我不相信这是个好主意: require ‘minitest/autorun’ require ‘minitest/mock’ module Hack class ClassDelegate def self.set_delegate(delegate); @@delegate = delegate; end def self.method_missing(sym, *args, &block) @@delegate.method_missing(sym, *args, &block) end end end class TestClassDelegation < MiniTest::Unit::TestCase class SomeModel < Hack::ClassDelegate ; end class SomeService def delete(id) SomeModel.delete(id) end end def test_delegation id = '123456789' mock = […]

Rack Session Cookie和Sinatra – 设置和访问数据

我正在使用机架会话池,但是我的用户会被一个网络服务器线程踢到另一个网络服务器线程上,使会话数据到期。 我开始在Sinatra中启用:会话,但是我无法使用它,因为我有使用Sinatra的多个应用程序(它似乎正在使用相同的密钥 – 不确定这是因为它是否是相同的主机) 因此,由于我的应用程序会相互破坏,我现在正在尝试使用Rack Session Cookie并设置变量(与启用相同:会话,但您可以设置变量) 伟大,所以有效! 但是现在我无法以我使用它的方式访问会话数据,在Rack Session Pool和enable:sessions中 session[:user] = nick puts session[:user] 你明白了…… 问题是为什么我可以在Pool和Sinatra中使用session [:user]启用会话数据,但是不能在Rack Session Cookie中访问会话数据? 我错过了什么吗? 我正在做的就是下面 config.ru use Rack::Session::Cookie, :key => ‘key’, :domain => “localhost”, :path => ‘/’, :expire_after => 14400, # In seconds :secret => ‘secret’ 编辑: 做了一些测试并发现它实际上把它放在会话变量中,但是一旦它移动到新方法或重定向会话变量似乎被删除(这个cookie真的大于4KB?!) – 它可以’因为启用:会话工作得很好

(在Ruby中)允许混合类方法访问类常量

我有一个定义了常量的类。 然后我定义了一个访问该类常量的类方法。 这很好用。 一个例子: #! /usr/bin/env ruby class NonInstantiableClass Const = “hello, world!” class << self def shout_my_constant puts Const.upcase end end end NonInstantiableClass.shout_my_constant 我的问题出现在尝试将此类方法移出到外部模块,如下所示: #! /usr/bin/env ruby module CommonMethods def shout_my_constant puts Const.upcase end end class NonInstantiableClass Const = “hello, world!” class << self include CommonMethods end end NonInstantiableClass.shout_my_constant Ruby将该方法解释为从模块而不是类中请求常量: line 5:in `shout_my_constant’: uninitialized […]

Ruby在数组中找到下一个

反正有没有找到Ruby数组中的下一个项目? 码: # Find ALL languages if !debug lang = Language.all else lang = Language.where(“id = ? OR id = ?”, 22, 32) end # Get all elements elements = Element.where(“human_readable IS NOT NULL”) lang.each do |l| code = l.code.downcase if File.exists?(file_path + code + “.yml”) File.delete(file_path + code + “.yml”) end t1 = Time.now […]

设计 – Rails 4 – 无法登录

我刚开始使用Rails 4.我使用自定义字段设置了devise 。 我可以在注册后登录,但我无法从登录页面登录。 用户注册很好,我在数据库表中username , password等。 有人能告诉我我错过了什么吗? 我的用户模型非常基础。 这是它的样子 – class User true, :presence => {:message => ‘- Username cannot be blank’} validates :first_name, :presence => { :message => ” – Firstname cannot be blank!”} private def user_params params.require().permit(:first_name, :last_name, :username, :email, :password, :password_confirmation) end end 我检查了我的观点,并且我传递了正确的参数。 我的应用程序控制器中有以下内容 – class ApplicationController < ActionController::Base # […]