form_forを使用し非表示のフォームを作成したのですがモデルに保存できなくて困っています。
binding.pryをしたところcreateアクションにparamsでデータが送られてきていることは確認できています。
####ターミナル↓
19: def create
=> 20: binding.pry
21: UserFood.create(user_food_params)
22: redirect_to root_path
23: end
[1] pry(#<OrdersController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"oEWDCU7hgArdapqijWBEwMF9hJJ2pAIQQBL7Pqnh68BW1sPDs7kxLY2aV4Mx56Rzo3qNsJAy7ZKQCq963ah5WQ==", "user_food"=>{"food"=>"りんご", "price"=>"400", "quantity"=>"1"}, "commit"=>"1", "controller"=>"orders", "action"=>"create"} permitted: false>
[2] pry(#<OrdersController>)> user_food_params
=> <ActionController::Parameters {"food"=>"りんご", "price"=>"400", "quantity"=>"1"} permitted: true>
[3] pry(#<OrdersController>)>
###controller
class OrdersController < ApplicationController def index @user_food= UserFood.new if @user_food.save else end @user_foods = UserFood.all price = UserFood.sum(:price) quantity = UserFood.sum(:quantity) @total = ((price * quantity)/2) end def new end def create UserFood.create(user_food_params) redirect_to root_path end def destroy order = UserFood.find(params[:id]) order.destroy redirect_to root_path end private def user_food_params params.require(:user_food).permit(:food, :price, :quantity) end end
###index.html.haml
.number-box__row #1.number-box__row--btn = form_for(@user_food, url: orders_path ) do |f| = f.hidden_field :food, value: "りんご" = f.hidden_field :price, value: 400 = f.hidden_field :quantity, value: 1 = f.submit "1", class: "btn"
回答1件
あなたの回答
tips
プレビュー