Rails3からRails5へアップデートの作業を行っています。
その際にストロングパラメータが有効になった影響でエラーが出てしまい、ネットで調べた解決方法ではエラーを治せなくこちらでご質問させてください。
Rails3でcreateメソッド登録する際に以下の実装にしていますが
def create @library = Library.new(params[:library]) @library.translations = params[:library_translations] respond_to do |format| if @library.save format.html { redirect_to(admin_library_url(@library), :notice => t('controller.successfully_created', :model => t('activerecord.models.library'))) } else format.html { render :action => "new" } end end end
Rails5からマスアサインメントが有効になっているため以下のように修正しています。
(パラメータの指定の前に動作確認がしたいのでpermit!して全許可しようとしてます)
def create param.permit! @library = Library.new(params[:library]) @library.translations = params[:library_translations] respond_to do |format| if @library.save format.html { redirect_to(admin_library_url(@library), :notice => t('controller.successfully_created', :model => t('activerecord.models.library'))) } else format.html { render :action => "new" } end end end
上記でpermitはスルー出来ると考えていたのですが、createにsubmitすると以下のようにエラーが出ます。
ActiveModel::ForbiddenAttributesError in Admin::LibrariesController#create ActiveModel::ForbiddenAttributesError Extracted source (around line #23): 21 def sanitize_for_mass_assignment(attributes) 22 if attributes.respond_to?(:permitted?) 23 raise ActiveModel::ForbiddenAttributesError if !attributes.permitted? 24 attributes.to_h 25 else 26 attributes Application Trace | Framework Trace | Full Trace vendor/bundle/ruby/2.6.0/gems/activemodel-5.2.0/lib/active_model/forbidden_attributes_protection.rb:23:in `sanitize_for_mass_assignment' vendor/bundle/ruby/2.6.0/gems/activemodel-5.2.0/lib/active_model/attribute_assignment.rb:35:in `assign_attributes' vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.0/lib/active_record/core.rb:314:in `initialize' vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.0/lib/active_record/inheritance.rb:66:in `new' vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.0/lib/active_record/inheritance.rb:66:in `new' vendor/bundle/ruby/2.6.0/gems/cancancan-3.0.1/lib/cancan/controller_resource_builder.rb:8:in `build_resource' vendor/bundle/ruby/2.6.0/gems/cancancan-3.0.1/lib/cancan/controller_resource_loader.rb:108:in `load_resource_instance' vendor/bundle/ruby/2.6.0/gems/cancancan-3.0.1/lib/cancan/controller_resource_loader.rb:18:in `load_resource' vendor/bundle/ruby/2.6.0/gems/cancancan-3.0.1/lib/cancan/controller_resource.rb:33:in `load_and_authorize_resource' vendor/bundle/ruby/2.6.0/gems/cancancan-3.0.1/lib/cancan/controller_resource.rb:17:in `block in add_before_action' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:426:in `instance_exec' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:426:in `block in make_lambda' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:198:in `block (2 levels) in halting' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:199:in `block in halting' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:513:in `block in invoke_before' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:513:in `each' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:513:in `invoke_before' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:131:in `run_callbacks' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/abstract_controller/callbacks.rb:41:in `process_action' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_controller/metal/rescue.rb:22:in `process_action' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/notifications.rb:168:in `block in instrument' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/notifications/instrumenter.rb:23:in `instrument' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/notifications.rb:168:in `instrument' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_controller/metal/instrumentation.rb:32:in `process_action' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_controller/metal/params_wrapper.rb:256:in `process_action' vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.0/lib/active_record/railties/controller_runtime.rb:24:in `process_action' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/abstract_controller/base.rb:134:in `process' vendor/bundle/ruby/2.6.0/gems/actionview-5.2.0/lib/action_view/rendering.rb:32:in `process' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_controller/metal.rb:191:in `dispatch' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_controller/metal.rb:252:in `dispatch' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:52:in `dispatch' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:34:in `serve' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/journey/router.rb:52:in `block in serve' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/journey/router.rb:35:in `each' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/journey/router.rb:35:in `serve' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:840:in `call' vendor/bundle/ruby/2.6.0/gems/warden-1.2.8/lib/warden/manager.rb:36:in `block in call' vendor/bundle/ruby/2.6.0/gems/warden-1.2.8/lib/warden/manager.rb:34:in `catch' vendor/bundle/ruby/2.6.0/gems/warden-1.2.8/lib/warden/manager.rb:34:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/tempfile_reaper.rb:15:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/etag.rb:25:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/conditional_get.rb:38:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/head.rb:12:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/http/content_security_policy.rb:18:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/cookies.rb:670:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:98:in `run_callbacks' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/callbacks.rb:26:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/executor.rb:14:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call' vendor/bundle/ruby/2.6.0/gems/railties-5.2.0/lib/rails/rack/logger.rb:38:in `call_app' vendor/bundle/ruby/2.6.0/gems/railties-5.2.0/lib/rails/rack/logger.rb:26:in `block in call' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/tagged_logging.rb:71:in `block in tagged' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/tagged_logging.rb:28:in `tagged' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/tagged_logging.rb:71:in `tagged' vendor/bundle/ruby/2.6.0/gems/railties-5.2.0/lib/rails/rack/logger.rb:26:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/remote_ip.rb:81:in `call' vendor/bundle/ruby/2.6.0/gems/request_store-1.4.1/lib/request_store/middleware.rb:19:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/request_id.rb:27:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call' vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.0/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/executor.rb:14:in `call' vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/static.rb:127:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call' vendor/bundle/ruby/2.6.0/gems/railties-5.2.0/lib/rails/engine.rb:524:in `call' vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/handler/webrick.rb:86:in `service' /usr/local/rbenv/versions/2.6.5/lib/ruby/2.6.0/webrick/httpserver.rb:140:in `service' /usr/local/rbenv/versions/2.6.5/lib/ruby/2.6.0/webrick/httpserver.rb:96:in `run' /usr/local/rbenv/versions/2.6.5/lib/ruby/2.6.0/webrick/server.rb:307:in `block in start_thread'
この場合createアクションにpermit!は間違っていますでしょうか?
解決策がわからず途方に暮れています。
ご教示いただけますと大変幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。