前提・実現したいこと
あるフリマアプリのクローンを作成しています。
商品出品ページを作成している段階です。
配送料が着払いか、元払いかによって配送方法の種類が変わるといった処理をajaxとjqueryを使用して実現しています。
上記の動的にセレクトボックスの中身を変更する処理はできています。
Productというモデルにdelivery_fee_owner_idというカラムを追加して、着払いか、元払かをcollection_selectで選択させてストロングパラメーターを使用して、delivery_fee_owner_idを許可しているのですが、なぜか
Unpermitted parameters:となってしまい、ROLLBACKしていまいます。
なぜパラメーターを許可しているのにUnpermitted parameters:となってしまうのか、ご教示いただきたく思います。
発生している問題・エラーメッセージ
Started POST "/sells" for 127.0.0.1 at 2019-04-04 00:29:30 +0900 Processing by SellsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"T3VM0FzDui3t+TU5oErMvEuPsSGjdtwOGxp2TDR04jEuBvxjl5x33nxoaHHujsvdDGPQi/mwjfGpgUWvfpsdJQ==", "image1"=>"", "product"=>{"name"=>"aaaaaa", "info"=>"aaaaaaaaaaa", "status"=>"新品未使用", "delivery_fee_owner_id"=>"1", "price"=>"2000"}, "shipping_method"=>"1", "commit"=>"出品する"} Unpermitted parameters: :name, :info, :status, :delivery_fee_owner_id, :price (4.9ms) BEGIN ↳ app/controllers/sells_controller.rb:14 DeliveryFeeOwner Load (3.5ms) SELECT `delivery_fee_owners`.* FROM `delivery_fee_owners` WHERE `delivery_fee_owners`.`id` = 1 LIMIT 1 ↳ app/controllers/sells_controller.rb:14 (0.2ms) ROLLBACK ↳ app/controllers/sells_controller.rb:14 Redirected to http://0.0.0.0:3001/ Completed 302 Found in 18ms (ActiveRecord: 8.6ms)
該当のソースコード
sellscontroller
1class SellsController < ApplicationController 2 def index 3 end 4 def new 5 @sell = Product.new 6 @shipping_method = ShippingMethod.new 7 end 8 9 def create 10 @sell = Product.new(sell_params) 11 @shipping_method = ShippingMethod.new(shipping_method_params) 12 if @sell.save && @shipping_method.save 13 redirect_to sell_path(@sell) 14 else 15 redirect_to root_path 16 end 17 end 18 19 def shipping_method 20 render partial: 'sells/shipping_method', locals: { delivery_fee_owner_id: params[:delivery_fee_owner_id] } 21 end 22 23 private 24 25 def sell_params 26 params.require(:product).permit(:delivery_fee_owner_id, :name, :info, :price, :status) 27 end 28 def shipping_method_params 29 params.require(:product).permit(:shipping_method_id) 30 end 31end
sell_html.haml
1%main.single-main 2 %section.sell-container 3 .sell-container__inner 4 %h2.single-container__head 5 商品の情報を入力 6 = form_with model: @sell,url:sells_path, class:"sell-container__form", local:true do |f| 7 = f.label "商品名" 8 = f.text_field :name,class: "sell-content__input input-default", placeholder: "商品名(必須 40文字まで)" 9 = f.label "商品の説明" 10 = f.text_area :info,class:"sell-content__desc_textarea textarea-default",rows:"5" ,placeholder:"商品の説明(必須 1,000文字以内)(色、素材、重さ、定価、注意点など)例)2010年頃に1万円で購入したジャケットです。ライトグレーで傷はありません。あわせやすいのでおすすめです。" 11 = f.label "カテゴリー" 12 = f.label "商品の状態" 13 = f.select :status, Product.statuses.keys, {}, class: "sell-content__select-default" 14 = f.label "配送料の負担" 15 = f.collection_select :delivery_fee_owner_id, DeliveryFeeOwner.all,:id, :delivery_fee_owner, {prompt: "---"},{class:"sell-content__select-default",id:"delivery_fee_owner" 16 //jqueryで:delivery_fee_ownersの値によって表示するセレクトボックスを変動させる。 17 = f.label "配送の方法" 18 = render partial: 'shipping_method',locals: {delivery_fee_owner_id: DeliveryFeeOwner.first.id} 19 = f.label "価格" 20 = f.text_field :price, class:"input-default",placeholder:"例)300" 21 = f.submit "出品する" ,class:"sell-content__btn-red"
sellcoffee
1$(document).on 'change', '#delivery_fee_owner', -> 2 $.ajax( 3 type: 'GET' 4 url: '/sells/shipping_method' 5 data: { 6 delivery_fee_owner_id: $(this).val() 7 } 8 ).done (data) -> 9 console.log(data) 10 $('.shipping-method').html(data)
routes
1Rails.application.routes.draw do 2 3 root 'products#index' 4 devise_for :users 5 resources :users, only: [:show] 6 resources :products 7 resources :sells do 8 get 'shipping_method', on: :collection 9 end 10 resources :buys 11end 12
試したこと
rails console上では当たり前ですが追加できました。
rails
1[[Airb(main):001:0> product = Product.new(name:"test",price:200,info:"test",shipping_method_id:1,delivery_fee_owner_id:1) 2 (0.9ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 3=> #<Product id: nil, name: "test", created_at: nil, updated_at: nil, price: 200, info: "test", status: nil, shipping_method_id: 1, delivery_fee_owner_id: 1> 4irb(main):002:0> product.save 5 (0.2ms) BEGIN 6 DeliveryFeeOwner Load (0.4ms) SELECT `delivery_fee_owners`.* FROM `delivery_fee_owners` WHERE `delivery_fee_owners`.`id` = 1 LIMIT 1 7 ShippingMethod Load (0.2ms) SELECT `shipping_methods`.* FROM `shipping_methods` WHERE `shipping_methods`.`id` = 1 LIMIT 1 8 Product Create (34.1ms) INSERT INTO `products` (`name`, `created_at`, `updated_at`, `price`, `info`, `shipping_method_id`, `delivery_fee_owner_id`) VALUES ('test', '2019-04-03 15:53:25', '2019-04-03 15:53:25', 200, 'test', 1, 1) 9 (4.6ms) COMMIT 10=> true
補足情報(FW/ツールのバージョンなど)
Rails 5.2.2
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
mac os mojave 10.14(18A391)
お手数かけますが、お願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。