前提・実現したいこと
簡単なECサイトを作成中です。
Formオブジェクトで購入と住所のテーブルに保存しようとしています。
商品購入フォームで、subimtした際、エラーがでます。
入力欄への入力はしっかりと行っております。
解決方法をご教示いただけましたら幸いです。
不足情報あれば、ご指摘ください。
##発生している問題・エラーメッセージ
param is missing or the value is empty: address_order
def order_params # ↓この部分 params.require(:address_order).permit(:postal_code, :city, :street, :house_name, :phone_number).merge(user_id: current_user.id, food_id: params[:food_id]) end
調べたこと
Googleで調べたところ、address_orderが無い又は空である。
・require(:address_order)があると保存はできるがそもそも商品購入フォームへいけなくなる(param is missing or the value is empty: address_orderとエラー)
・require(:address_order)が無いと商品購入フォームへ行けるがsubmit時に保存出来ない
##該当のソースコード
model
class AddressOrder include ActiveModel::Model attr_accessor :postal_code, :city, :street, :house_name, :phone_number, :user_id, :food_id # 省略 def save order = Order.create(food_id: food_id, user_id: user_id) Address.create(postal_code: postal_code, city: city, street: street, house_name: house_name, phone_number: phone_number) end end
class Address < ApplicationRecord belongs_to :order end
class Order < ApplicationRecord belongs_to :user belongs_to :food has_one :address end
class Food < ApplicationRecord belongs_to :user has_one_attached :image has_one :order # 省略 end
routes
Rails.application.routes.draw do devise_for :users root to: "foods#index" resources :foods do resources :orders, only: [:create, :index] end end
controller
class OrdersController < ApplicationController before_action :authenticate_user!, only: [:index] before_action :set_food, only: [:index, :create] def index @address_order = AddressOrder.new end def create @address_order = AddressOrder.new(order_params) if @address_order.valid? @address_order.save redirect_to root_path else render action: :index end end private def order_params params.require(:address_order).permit(:postal_code, :city, :street, :house_name, :phone_number).merge(user_id: current_user.id, food_id: params[:food_id]) end def set_food @food = Food.find(params[:food_id]) end end
view
<%= form_with model: @address_order, url: food_orders_path, local: true do |f| %> <%= f.text_field :postal_code, maxlength:"8" %> <%= f.text_field :city %> <%= f.text_field :street %> <%= f.text_field :house_name %> <%= f.text_field :phone_number, maxlength:"11"%> <%= f.submit "購入" %> <% end %>
log
Started GET "/foods/1" for ::1 at 2020-12-13 13:28:58 +0900 Processing by FoodsController#show as HTML Parameters: {"id"=>"1"} [1m[36mFood Load (0.4ms)[0m [1m[34mSELECT `foods`.* FROM `foods` WHERE `foods`.`id` = 1 LIMIT 1[0m ↳ app/controllers/foods_controller.rb:56:in `set_food' Rendering foods/show.html.erb within layouts/application [1m[36mUser Load (0.4ms)[0m [1m[34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1[0m ↳ app/views/shared/_header.html.erb:15 Rendered shared/_header.html.erb (Duration: 4.7ms | Allocations: 2467) [1m[36mActiveStorage::Attachment Load (0.4ms)[0m [1m[34mSELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'Food' AND `active_storage_attachments`.`name` = 'image' LIMIT 1[0m ↳ app/views/foods/show.html.erb:11 [1m[36mActiveStorage::Blob Load (0.3ms)[0m [1m[34mSELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 1 LIMIT 1[0m ↳ app/views/foods/show.html.erb:11 Rendered foods/show.html.erb within layouts/application (Duration: 9.5ms | Allocations: 4794) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 90ms (Views: 86.0ms | ActiveRecord: 1.5ms | Allocations: 25558) Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhOMlF4YTJGaWFHYzFOR2czWjNoMU1XRXhkM0JtT0hkNGNUUnRlUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpUTJsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGNtbDVZV3RwTG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25kR1Z5YVhsaGEya3VhbkJuQmpzR1ZEb1JZMjl1ZEdWdWRGOTBlWEJsU1NJUGFXMWhaMlV2YW5CbFp3WTdCbFE9IiwiZXhwIjoiMjAyMC0xMi0xM1QwNDozMTozNC43MTFaIiwicHVyIjoiYmxvYl9rZXkifX0=--aa418a0630aa9d3b3d01b135de8251b42140df17/teriyaki.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22teriyaki.jpg%22%3B+filename%2A%3DUTF-8%27%27teriyaki.jpg" for ::1 at 2020-12-13 13:28:58 +0900 Processing by ActiveStorage::DiskController#show as JPEG Parameters: {"content_type"=>"image/jpeg", "disposition"=>"inline; filename=\"teriyaki.jpg\"; filename*=UTF-8''teriyaki.jpg", "encoded_key"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDRG9JYTJWNVNTSWhOMlF4YTJGaWFHYzFOR2czWjNoMU1XRXhkM0JtT0hkNGNUUnRlUVk2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpUTJsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW5SbGNtbDVZV3RwTG1wd1p5STdJR1pwYkdWdVlXMWxLajFWVkVZdE9DY25kR1Z5YVhsaGEya3VhbkJuQmpzR1ZEb1JZMjl1ZEdWdWRGOTBlWEJsU1NJUGFXMWhaMlV2YW5CbFp3WTdCbFE9IiwiZXhwIjoiMjAyMC0xMi0xM1QwNDozMTozNC43MTFaIiwicHVyIjoiYmxvYl9rZXkifX0=--aa418a0630aa9d3b3d01b135de8251b42140df17", "filename"=>"teriyaki"} Completed 304 Not Modified in 2ms (ActiveRecord: 0.0ms | Allocations: 728) Started POST "/foods/1/orders" for ::1 at 2020-12-13 13:29:02 +0900 Processing by OrdersController#create as HTML Parameters: {"authenticity_token"=>"VgUl7/gmHxJZtJ0Nm4uuRp+MCFNYqE3o5Q+gXG6SzQCVDwNxbLnO4BbseoNR1L738ou63i7lt6TahMmTqTRJGg==", "food_id"=>"1"} [1m[36mFood Load (0.5ms)[0m [1m[34mSELECT `foods`.* FROM `foods` WHERE `foods`.`id` = 1 LIMIT 1[0m ↳ app/controllers/orders_controller.rb:34:in `set_food' Completed 400 Bad Request in 5ms (ActiveRecord: 0.5ms | Allocations: 1634) ActionController::ParameterMissing (param is missing or the value is empty: address_order): app/controllers/orders_controller.rb:24:in `order_params' app/controllers/orders_controller.rb:11:in `create'
show.html.erb
# 省略 <% if user_signed_in? %> <% if current_user.id == @food.user_id %> <%= link_to '商品の編集', edit_food_path(@food.id), method: :get, class: "food-btn" %> <%= link_to '削除', "/foods/#{@food.id}", method: :delete, class:'food-destroy' %> <% else %> <%= link_to '購入する',food_orders_path(@food.id), class:"food-btn", method: :post%> <% end %> <% end %> # 省略
あなたの回答
tips
プレビュー