Rails 5 fields_for классов пространства имен - PullRequest
0 голосов
/ 12 ноября 2018

У меня возникают проблемы с вложенными атрибутами, когда определенные классы находятся внутри модуля:

# file bar.rb
module Abc
    class Bar << ActiveRecord::Base
        has_many :foos
        accepts_nested_attributes_for :foos
    end
end

# file foo.rb
module Abc
    class Foo << ActiveRecord::Base
        belongs_to :bar
        # let's consider an attribute: qux
    end
end

# inside the controller
def new
    @bar = Abc::Bar.new
end

# inside a view
<%= form_for @bar .... do |f| %>
    ....
    <%= f.fields_for :abc_foos, @bar.foos do |ff| %>
         <%= ff.hidden_field :qux, value: true %>
    <% end %>
<% end %>

Теперь мы получили префикс "abc" в именах атрибутов>

# the rendered html
<input type="hidden"  name="abc_bar[abc_foos][qux]" ... />

Параметры теперь поставляются с включенным abc_foos:

 # the params hash
    "abc_bar" => {
        ....
       "abc_foos" => {"qux" => "some value"}
    }

Проблема: метод accepts_nested_attributes_for создает что-то вроде «abc_foos_attributes =» для получения атрибутов, но имя ключа параметра по умолчанию другое. Итак, это не будет работать:

 # in the controller, again
 def create
       @bar = Abc::Bar.new(params.require(:abc_bar).permit(:a, :b, abc_foos: [:qux]))
 end

Я получаю ошибку:

unknown attribute 'abc_foos' for Abc::Bar.

Я бы ожидал что-то вроде "foos_attributes" в качестве подходящего ключа. Чего мне не хватает?

Спасибо!

Полная бактраса:

activemodel (5.2.1) lib/active_model/attribute_assignment.rb:53:in `_assign_attribute'
activerecord (5.2.1) lib/active_record/attribute_assignment.rb:31:in `block in assign_nested_parameter_attributes'
activerecord (5.2.1) lib/active_record/attribute_assignment.rb:31:in `each'
activerecord (5.2.1) lib/active_record/attribute_assignment.rb:31:in `assign_nested_parameter_attributes'
activerecord (5.2.1) lib/active_record/attribute_assignment.rb:25:in `_assign_attributes'
activemodel (5.2.1) lib/active_model/attribute_assignment.rb:35:in `assign_attributes'
activerecord (5.2.1) lib/active_record/core.rb:314:in `initialize'
activerecord (5.2.1) lib/active_record/inheritance.rb:66:in `new'
activerecord (5.2.1) lib/active_record/inheritance.rb:66:in `new'
app/controllers/bar_controller.rb:33:in `create'
actionpack (5.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.1) lib/abstract_controller/base.rb:194:in `process_action'
actionpack (5.2.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.2.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (5.2.1) lib/active_support/callbacks.rb:132:in `run_callbacks'
actionpack (5.2.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.1) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.1) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.1) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.1) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.1) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.1) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.1) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.1) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.1) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.1) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.1) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.1) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.1) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.1) lib/action_dispatch/routing/route_set.rb:840:in `call'
rack (2.0.6) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
actionpack (5.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.6) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.6) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/cookies.rb:670:in `call'
activerecord (5.2.1) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.1) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
railties (5.2.1) lib/rails/engine.rb:524:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'    

1 Ответ

0 голосов
/ 12 ноября 2018

Можете ли вы публиковать полные сообщения об ошибках? Но тем временем, если вы попробуете

<%= f.fields_for :foos, @bar.foos do |foo| %>
     <%= foo.hidden_field :qux, value: true %>
<% end %>

Вам, вероятно, также нужно сделать

@bar = Abc::Bar.new(params.require(:abc_bar).permit(:a, :b, foos: [:qux]))
...