国家选择抛出ArgumentError

时间:2013-11-14 11:00:20

标签: ruby-on-rails ruby ruby-on-rails-4

我需要一个国家选择器。 Rails的网站写道,他们删除了他们的国家选择器助手,以免冒犯人。我安装了国家选择的宝石。它安装得很好,但当我这样称呼时:

f.country_select("country")

或者像这样:

f.country_select(:country)

其中f是我的FormBuilder对象,它会抛出ArgumentError

wrong number of arguments(4 for 0)

文档说它只需要一个参数。是什么给了这个? 4 for 0是什么意思?

编辑: 这是完整的错误堆栈:

country-select (1.1.1) lib/country-select.rb:7:in `initialize'
country-select (1.1.1) lib/country-select.rb:7:in `new'
country-select (1.1.1) lib/country-select.rb:7:in `country_select'
country-select (1.1.1) lib/country-select.rb:93:in `country_select'
app/views/admin_area/new_user.html.erb:15:in `block in _app_views_admin_area_new_user_html_erb__169470495_30991140'
actionpack (4.0.1) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.1) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.1) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.1) lib/action_view/helpers/form_helper.rb:435:in `form_for'
app/views/admin_area/new_user.html.erb:2:in `_app_views_admin_area_new_user_html_erb__169470495_30991140'
actionpack (4.0.1) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.1) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.1) lib/action_view/template.rb:141:in `render'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
actionpack (4.0.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.0.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:48:in `block in render_template'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:56:in `render_with_layout'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:47:in `render_template'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionpack (4.0.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionpack (4.0.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:127:in `_render_template'
actionpack (4.0.1) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:120:in `render_to_body'
actionpack (4.0.1) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.1) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/Ruby193/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
activesupport (4.0.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.0.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.0.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.0.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.0.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.1) lib/active_support/callbacks.rb:403:in `_run__836640127__process_action__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.1) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:680:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.1) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.1) lib/active_support/callbacks.rb:373:in `_run__1017747328__call__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.1) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.1) lib/rails/engine.rb:511:in `call'
railties (4.0.1) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
C:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
C:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
C:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

这就是我所说的地方:

    <div id="new_user_form">
  <%= form_for(:user, :url => {:action =>'create_user'}) do |f| %>
       <table>
         <tr><td><%= f.label(:username, 'Enter a Username:') %> </td>
     <td><%= f.text_field(:username) %></td> </tr>
     <tr><td> <%= f.label(:password, 'Enter a Password:') %></td>
      <td><%= f.password_field(:password) %></td></tr>
      <tr><td><%= f.label(:confirm, 'Confirm your password:') %></td>
      <td><%= f.password_field(:confirm) %></td></tr>
     <tr><td> <%= f.label(:email, 'Enter your e-mail address:') %></td>
     <td> <%= f.text_field(:email) %></td></tr>
      <tr><td><%= f.label(:username, 'City:') %></td>
      <td><%= f.text_field(:city) %></td></tr>
      <tr><td><%= f.label(:country, 'Country:') %></td>
      <td><%= f.country_select(:country) %></td></tr>
    <tr> <td> <%= f.label(:firstname, 'First Name:') %></td>
    <td>  <%= f.text_field(:firstname) %></td></tr>
      <tr><td><%= f.label(:lastname, 'Last Name:') %></td>
      <td><%= f.text_field(:lastname) %></td></tr>
      <tr><td><%= f.submit('Submit') %></td><td></td></tr>
       </table>
      <% end  %>
</div>

(这仍然是一项正在进行中的工作,顺便说一句)

这是country-select.rb(不是我的代码)

# CountrySelect
module ActionView
  module Helpers
    module FormOptionsHelper
      # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
      def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
        InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
      end
      # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
      # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so
      # that they will be listed above the rest of the (long) list.
      #
      # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
      def country_options_for_select(selected = nil, priority_countries = nil)
        country_options = ""

        if priority_countries
          if (unlisted = priority_countries - COUNTRIES).any?
            raise RuntimeError.new("Supplied priority countries are not in the main list: #{unlisted}")
          end
          country_options += options_for_select(priority_countries, selected)
          country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"

          # prevents selected from being included twice in the HTML which causes
          # some browsers to select the second selected option (not priority)
          # which makes it harder to select an alternative priority country
          selected = nil if priority_countries.include?(selected)
        end

        country_options = country_options.html_safe if country_options.respond_to?(:html_safe)

        return country_options + options_for_select(COUNTRIES, selected)
      end

      # All the countries included in the country_options output.
      COUNTRIES = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola",
        "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria",
        "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
        "Bermuda", "Bhutan", "Bolivia, Plurinational State of", "Bonaire, Sint Eustatius and Saba", "Bosnia and Herzegovina",
                "Botswana", "Bouvet Island", "Brazil",
        "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
        "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
        "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
        "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba",
        "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt",
        "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)",
        "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia",
        "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
                "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea",
        "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Holy See (Vatican City State)",
        "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq",
        "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya",
        "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan",
        "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
        "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, The Former Yugoslav Republic Of",
        "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
        "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
        "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru",
        "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
        "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau",
        "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
        "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation",
        "Rwanda", "Saint Barthelemy", "Saint Helena, Ascension and Tristan da Cunha", "Saint Kitts and Nevis", "Saint Lucia",
        "Saint Martin (French Part)", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
        "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore",
        "Sint Maarten (Dutch Part)", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa",
        "South Georgia and the South Sandwich Islands", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname",
        "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic",
        "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Timor-Leste",
        "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
        "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
        "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu",
                "Venezuela, Bolivarian Republic of", "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.",
                "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"] unless const_defined?("COUNTRIES")
    end

    class InstanceTag
      def to_country_select_tag(priority_countries, options, html_options)
        html_options = html_options.stringify_keys
        add_default_name_and_id(html_options)
        value = value(object)
        content_tag("select",
          add_options(
            country_options_for_select(value, priority_countries),
            options, value
          ), html_options
        )
      end
    end

    class FormBuilder
      def country_select(method, priority_countries = nil, options = {}, html_options = {})
        @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
      end
    end
  end
end

4 个答案:

答案 0 :(得分:3)

检查country_select gem的版本。它似乎最近更新为符合Rails 4.x中引入的更改。你需要v1.1.1或更高版本。

答案 1 :(得分:1)

不幸的是,关于此的文档有点稀疏。 当您的country_select位于表单中时(即您使用的是f.country_select ...),由于您已经拥有object,因此无需提供form_for(object)

我在Rails 3.2.17和country_select 1.1.3中成功使用了以下语法:

form_for(myobject) do |f|
  ...
  f.country_select "country", ["Australia", "New Zealand"], {}, {class: "form-control", id: "country"}
  ...
end

"country"myobject的属性,澳大利亚和新西兰是优先国家/地区,空{}是选项,其余是html_options。

答案 2 :(得分:0)

尝试更改视图而不是

f.country_select('country') 

DO

  country_select(:user, :country)

答案 3 :(得分:0)

我必须安装另一个gem来解决Rails版本4.2.0中的相同错误:

wrong number of arguments(4 for 0)

gem指定为:

gem 'country_select', github: 'stefanpenner/country_select'
在您的Gemfile中

,然后运行bundle install并重新启动服务器。我找到了这个解决方案here