Cześć
Chcę dodać w widoku pola wyboru kraju, stanu, miasta uzywając gem-a city_state (przy gem coutry_state_select jest ten błąd). O ile bez problemu tworzy się lista rozwijalna z państwami:
<td> <%= form.select :country, CS.get, class: "form-control" %></td>
to próba dodania regionu(states):
<td> <%= form.select :vivodeship, CS.states(country), class: "form-control" %></td>
wywołuje błąd jak w temacie.
Będę wdzięczny za wskazówki, co może być przyczyną.
Wklej cały stacktrace (backtrace/callstack) z błędu.
Aplikacja nie wie co to country
. Prawdopodobnie chcesz ze zmiennej instancji jaką użyłeś w form_for
wyciągać country
więc powinno być coś na wzór
CS.states(@sth.country)
zamiast
CS.states(country)
lub statycznie wpisać konkretny kraj
CS.states(:us)
app/views/available_coaches/new.html.erb:109
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.1.3.2) lib/action_view/helpers/form_helper.rb:450:in `form_for'
app/views/available_coaches/new.html.erb:4
actionview (6.1.3.2) lib/action_view/base.rb:247:in `public_send'
actionview (6.1.3.2) lib/action_view/base.rb:247:in `_run'
actionview (6.1.3.2) lib/action_view/template.rb:154:in `block in render'
activesupport (6.1.3.2) lib/active_support/notifications.rb:205:in `instrument'
actionview (6.1.3.2) lib/action_view/template.rb:345:in `instrument_render_template'
actionview (6.1.3.2) lib/action_view/template.rb:152:in `render'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:61:in `block (2 levels) in render_template'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:56:in `block in render_template'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:71:in `block in render_with_layout'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:70:in `render_with_layout'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:55:in `render_template'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:11:in `render'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.1.3.2) lib/action_view/rendering.rb:117:in `block in _render_template'
actionview (6.1.3.2) lib/action_view/base.rb:273:in `in_rendering_context'
actionview (6.1.3.2) lib/action_view/rendering.rb:116:in `_render_template'
actionpack (6.1.3.2) lib/action_controller/metal/streaming.rb:218:in `_render_template'
actionview (6.1.3.2) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.1.3.2) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.1.3.2) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.1.3.2) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.1.3.2) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
/usr/share/rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
activesupport (6.1.3.2) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:86:in `cleanup_view_runtime'
activerecord (6.1.3.2) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:45:in `render'
actionpack (6.1.3.2) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.1.3.2) lib/abstract_controller/base.rb:228:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.1.3.2) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
actiontext (6.1.3.2) lib/action_text/rendering.rb:20:in `with_renderer'
actiontext (6.1.3.2) lib/action_text/engine.rb:55:in `block (4 levels) in <class:Engine>'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:126:in `instance_exec'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:137:in `run_callbacks'
actionpack (6.1.3.2) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:33:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
activerecord (6.1.3.2) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.1.3.2) lib/abstract_controller/base.rb:165:in `process'
actionview (6.1.3.2) lib/action_view/rendering.rb:39:in `process'
actionpack (6.1.3.2) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.1.3.2) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.1.3.2) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.1.3.2) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.1.3.2) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (6.1.3.2) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.1.3.2) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.1.3.2) lib/action_dispatch/routing/route_set.rb:842:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (6.1.3.2) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
actionpack (6.1.3.2) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/cookies.rb:689:in `call'
activerecord (6.1.3.2) lib/active_record/migration.rb:601:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (6.1.3.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.1.3.2) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.1.3.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.1.3.2) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (6.1.3.2) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (6.1.3.2) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (6.1.3.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.1.3.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/host_authorization.rb:98:in `call'
rack-mini-profiler (2.3.0) lib/mini_profiler/profiler.rb:373:in `call'
webpacker (5.2.1) lib/webpacker/dev_server_proxy.rb:25:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.1.3.2) lib/rails/engine.rb:539:in `call'
puma (5.1.1) lib/puma/configuration.rb:246:in `call'
puma (5.1.1) lib/puma/request.rb:76:in `block in handle_request'
puma (5.1.1) lib/puma/thread_pool.rb:337:in `with_force_shutdown'
puma (5.1.1) lib/puma/request.rb:75:in `handle_request'
puma (5.1.1) lib/puma/server.rb:431:in `process_client'
puma (5.1.1) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
Jesli wpiszę statycznie kraj, tak poradziłeś, to działa bez problemu. Natomiast jeszcze mam problem z dostaniem listy dla wszystkich krajów, nawet jak wywołam country na zmiennej z form for, tak jak radziłeś.
Musisz dokładniej opisać co masz na myśli pisząc lista dla wszystkich krajów.
Metoda CS.states
zwraca stany dla konkretnego kraju, żeby zwrócić dla każdego kraju wszystkie stany i dla każdego kraju wszystkie miasta musisz iterować w pętlach. Tylko nie za bardzo rozumiem sens takiej konstrukcji w formularzu.
Potrzeba osobny pól: państwo, stan, miasto i w zależności jakie państwo wybierzesz lista stanów powinna odświeżać się z nową listą stanów.
Sorki, faktycznie mocno nieprecyzjnie odpisałem. Miałem na myśli to, że jeśli zrobię tak jak napisałeś:
CS.states(@sth.country)
(wywołuję metodę country na zmiennej użytej w form_for), to wciąż dostaję ten sam błąd.Spodziewałem się, że jej wywołanie da mi listę wszystkich stanów/miast zawartych w gem-ie (stąd pisałem o “wszystkich”) i zostanie ona ograniczona dopiero przez kod JS - nie wiem, czy dobrze myślę?
Stąd też moje pytanie: czy metoda coutry jest wbudowana w gem, czy też trzeba ją jakoś osobno definiować? P.S.Oczywiście ogólny zarys formularza jest dokładnie taki, jak napisałeś.Przynam, że nie do końca rozumiem, jak ten gem działa “od środka”.
Country to nie metoda gema a twoja zmienna. Czy ty chcesz wyświetlać listę stanów dla pojedynczego kraju, cały czas tego samego? Czy w zależności jaki masz kraj zapisany gdzieś w bazie danych chcesz wyświetlać stany dynsmicznie. Musisz pokazać jakiś kod albo dokładniej opisać co chcesz osiągnąć, bo bez tego to tylko teoretyzujemy.