简体   繁体   English

Rails 2.3中的`flash.discard`失败,因为flash是一个Hash,而不是FlashHash

[英]`flash.discard` in Rails 2.3 fails because flash is a Hash, not a FlashHash

Given a functional test such as: 进行功能测试,例如:

def test_exciting_rails_upgrades
    login(m=users(:manager))

    post :import_users_map_fields, :csv_file => fixture_file_upload('/users.csv', 'text/csv')
    assert flash.empty?
    flash.discard
    # other goodies omitted

end

In Rails 2.3.2 there are no errors, however in 2.3.15 the error is: 在Rails 2.3.2中没有错误,但是在2.3.15中错误是:

NoMethodError: undefined method `discard' for {}:Hash
    /test/functional/broken_upgrades.rb:119:in `test_exciting_rails_upgrades'

Why is flash a Hash class instead of a FlashHash ? 为什么flashHash类而不是FlashHash

From the source it looks like both 2.3.2 and 2.3.15 ActionPack files lib/action_controller/flash.rb create the FlashHash class and inherit from Hash . 从源代码上看,Action Pack文件2.3.2和2.3.15都在lib/action_controller/flash.rb创建FlashHash类并从Hash继承。 However what is shown in this functional test in both 2.3.2 and 2.3.15 is a Hash class, not a HashFlash , so one cannot call discard on it. 但是,在此功能测试中在2.3.2和2.3.15中显示的是Hash类,而不是HashFlash ,因此无法对其调用调用。

Can anyone else reproduce this error with 2.3.15 and flash.discard ? 其他任何人都可以使用2.3.15和flash.discard重现此错误吗?

Here are two test cases you can use to prove ActionController changes the type of 'flash' depending on whether or not it is already set. 您可以使用以下两个测试用例来证明ActionController根据是否已设置'flash'来更改其类型。

In my app, you cannot see :index unless you're logged in, so in test_flash_is_now_a_flashhash you see that flash was set by the backend properly, while in test_flash_is_a_plain_hash it was not. 在我的应用中,除非登录,否则无法看到:index,因此在test_flash_is_now_a_flashhash中,您可以看到后端正确设置了闪存,而在test_flash_is_a_plain_hash中则没有。

def test_flash_is_a_plain_hash
  login(users(:permitted_user))
  get :index
  assert flash.instance_of?(Hash)
end

def test_flash_is_now_a_flashhash
  get :index
  assert_redirected_to :controller => "login"
  assert flash.instance_of?(ActionController::Flash::FlashHash)
end

You can see this for yourself in the ActionController::TestRequest code: 您可以在ActionController :: TestRequest代码中自己看到此代码:

def flash
    session['flash'] || {}
end

Update: This has been fixed in Rails branch 2-3-stable . 更新:这已在Rails分支2-3-stable中 修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM