2

国セレクターが必要です。Rails のサイトは、人々を怒らせないように国選択ヘルパーを削除したと書いています。国選択の宝石をインストールしました。正常にインストールされましたが、次のように呼び出すと:

f.country_select("country")

またはそのように:

f.country_select(:country)

f私のFormBuilderオブジェクトはどこですか、それはスローしますArgumentError

wrong number of arguments(4 for 0)

ドキュメントによると、必要な引数は1つだけです。何がこれを与えるのですか?とは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

4 に答える 4

3

country_select gem のバージョンを確認してください。Rails 4.x で導入された変更に準拠するために、かなり最近更新されたようです。v1.1.1 以上が必要です。

于 2013-11-14T14:09:50.240 に答える
1

残念ながら、これに関するドキュメントはややまばらです。フォーム内にある場合country_select(つまり、... を使用している場合)、既に があるため、 をf.country_select指定する必要はありません。objectform_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 です。

于 2014-05-03T02:02:31.500 に答える
0

見方を変えてみる

代わりに

f.country_select('country') 

行う

  country_select(:user, :country)
于 2013-11-14T13:19:29.970 に答える
0

Rails バージョン 4.2.0 で同じエラーを解決するには、別の gem をインストールする必要がありました。

wrong number of arguments(4 for 0)

gem は次のように指定されます。

gem 'country_select', github: 'stefanpenner/country_select'

Gemfile で、サーバーを実行bundle installして再起動します。この解決策はこちらで見つかりました。

于 2015-01-30T15:41:34.083 に答える