Rails flash 拋出錯誤數量的參數(0 代表 2)錯誤 (Rails flash is throwing wrong number of arguments (0 for 2) error)


問題描述

Rails flash 拋出錯誤數量的參數(0 代表 2)錯誤 (Rails flash is throwing wrong number of arguments (0 for 2) error)

I just upgraded an app from rails 1.2.3 to rails 2.0.2 (I tried 2.3.2 first and failed).  I had to make a few plugin tweaks, but overall went smoothly.  That is, until I realized my flashes weren't working.  So I then downgraded back to 1.2.3, and still receive the following error:

ActionView::TemplateError (wrong number of arguments (0 for 2))

Coming from wherever I first call flash in a view

<%= render :partial => "flash_msg", :object => flash %>

The flash_msg template is:

<% if flash[:notice] %>
    <div class="notice"><%= flash[:notice] %></div>
<% elsif flash[:warning] %>
    <div class="warning"><%= flash[:warning] %></div>
<% end %>

The full trace is:

ActionView::TemplateError (wrong number of arguments (0 for 2)) on line #53 of layouts/store.rhtml:
50: 
51:             <div class="col-left">
52:                 
53:                 <%= render :partial => "user/flash_msg", :object => flash %>
54:         
app/views/layouts/store.rhtml:53:in `flash'
app/views/layouts/store.rhtml:53:in `_run_erb_47app47views47layouts47store46rhtml'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:637:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:637:in `compile_and_render_template'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:365:in `render_template'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_view/base.rb:316:in `render_file'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/layout.rb:268:in `render_without_benchmark'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:51:in `render'
/opt/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:51:in `render'
app/controllers/store_controller.rb:75:in `browse'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform_action_without_filters'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in `call_filters'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/opt/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in `perform_action'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in `cache'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in `perform_action'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `process_without_filters'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in `process_without_session_management_support'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in `process'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in `process'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in `handle_request'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in `process'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `synchronize'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in `process'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:in `process_client'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `each'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in `process_client'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `initialize'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `new'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `initialize'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `new'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:271:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `each'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb:211:in `run'
/opt/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:489:in `load'
/opt/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/servers/mongrel.rb:64
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require'
/opt/local/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/server.rb:39
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3

參考解法

方法 1:

One possibility is that something (or someone) may have defined a flash method which is overriding the default flash behaviour. I.e. somewhere a flash method is defined which does take 2 parameters and where you have flash[:notice] it is calling the flash method and failing.

方法 2:

<p>@Shadwell, you were absolutely correct!  There was a view helper function named "flash" that rails 2+ does not like!  </p>

So I learned that a helper function named "flash" in Rails 1.2.3 will not override the flash hash.  In Rails 2+ it will.  

方法 3:

Try to remove ":object => flash". I think your partial will have access to flash without passing it as :object. 

方法 4:

I do that and the error just moves to the flash_msg partial:

wrong number of arguments (0 for 2)

Extracted source (around line #1):

1: <% if flash[:notice] %>
2:  <div class="notice"><%= flash[:notice] %></div>
3: <% elsif flash[:warning] %>
4:  <div class="warning"><%= flash[:warning] %></div>

方法 5:

After some more research, I see that as soon as it renders the view, flash no longer works.  For instance, flash.class throughout the controller returns:

ActionController::Flash::FlashHash

But as soon as I try to get flash.class in a view, I get the same error above.

And if I do give it two arguments, it returns "String."  For instance:

flash(nil, nil).class = String

That help anyone?

(by Mike SchmidtShadwellMike SchmidtklewMike SchmidtMike Schmidt)

參考文件

  1. Rails flash is throwing wrong number of arguments (0 for 2) error (CC BY-SA 3.0/4.0)

#upgrade #ruby-on-rails






相關問題

我如何知道哪個應用程序版本正在被 android 替換? (How do I know which app version is being replaced with android?)

升級到 Mac OS Lion - 如何備份 Ruby、*SQL 等? (Upgrade to Mac OS Lion - how to backup Ruby, *SQL, etc?)

將 jQuery 1.4.2 升級到 1.9.1 (Upgrade jQuery 1.4.2 to 1.9.1)

將 Jquery.min 升級到 Jquery.1.9 時出錯 (Error Upgrade Jquery.min into Jquery.1.9)

Building Wix 3.0 升級和補丁 (Building Wix 3.0 upgrade and patch)

Rails flash 拋出錯誤數量的參數(0 代表 2)錯誤 (Rails flash is throwing wrong number of arguments (0 for 2) error)

操作系統升級後無法從 ANT 訪問環境變量 (Can't access env vars from ANT after OS upgrade)

從 Rails 1.x 轉換到 2.x 是否有任何機械幫助? (Is there any mechanical assistance in converting from Rails 1.x to 2.x?)

Team Foundation Server 2015 到 2018 升級錯誤(步驟 909) (Team Foundation Server 2015 to 2018 Upgrade Error (Step 909))

運行 Tensorflow2 升級腳本會觸發編碼錯誤 (Running the Tensorflow2 upgrade Script triggers an encoding error)

將 Angular 5 升級到 Angular 11 (Upgrade angular 5 to angular 11)

在 centOS 6.9 (Final) 中將 php-5.6 升級到 7.3 時顯示多個錯誤 (Showing multiple errors while upgrading php-5.6 to 7.3 in centOS 6.9 (Final))







留言討論