前提・実現したいこと
profileとcouponの間にcoupon_profileを作って、このテーブルをこの記事でいう、likeと同じ扱いで、couponのshowにいいねボタンを作りたいと思っています。(このサービス上ではclipと呼んでいます。)
どのように修正すれば保存されるようになるでしょうか?
私としては
・Started POST "/clipped_coupons.2"
・#<CouponProfile ・・・ coupon_id: nil,
・(0.1ms) rollback transaction
この三点が気になっています。
発生している問題・エラーメッセージ
エラーメッセージではないですが、rails sの表示
Started POST "/clipped_coupons.2" for ::1 at 2019-04-23 14:06:36 +0900 Processing by ClippedCouponsController#create as Parameters: {"authenticity_token"=>"j3ZqIJxvLKA6OwynHKq14I3Fqrxwv7glZejsfxrcE5x49awMybaE8LHglQlEGeJzehWM75kb1+jXtpZrh/Cgsw=="} User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ vendor/bundle/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 Profile Load (0.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 1]] ↳ app/controllers/clipped_coupons_controller.rb:7 <ActionController::Parameters {"authenticity_token"=>"t4M4qj57R2DCduU8i80bubF+dYjnIADiWckw8w67y0ZAAP6Ga6LvMEmtfJLTfkwqRq5T2w6Eby/rl0rnk5d4aQ==", "controller"=>"clipped_coupons", "action"=>"create", "format"=>"2"} permitted: false> #<CouponProfile id: nil, coupon_id: nil, profile_id: 4, created_at: nil, updated_at: nil> (0.0ms) begin transaction ↳ app/controllers/clipped_coupons_controller.rb:9 Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]] ↳ app/controllers/clipped_coupons_controller.rb:9 CouponProfile Exists (0.2ms) SELECT 1 AS one FROM "coupon_profiles" WHERE "coupon_profiles"."coupon_id" IS NULL AND "coupon_profiles"."profile_id" = ? LIMIT ? [["profile_id", 4], ["LIMIT", 1]] ↳ app/controllers/clipped_coupons_controller.rb:9 (0.1ms) rollback transaction ↳ app/controllers/clipped_coupons_controller.rb:9 Redirected to http://localhost:3000/coupons/2 Completed 302 Found in 11ms (ActiveRecord: 1.3ms)
該当のソースコード
ruby
1class ClippedCouponsController < ApplicationController 2 def index 3 @clipped_coupons = current_user.profile.coupons 4 end 5 6 def create 7 @coupon_profile = CouponProfile.new(coupon_id: params[:coupon_id], profile_id: current_user.profile.id) 8 p params 9 p @coupon_profile 10 @coupon_profile.save 11 redirect_back(fallback_location: root_path) 12 end
html
1coupons/show.html 2<div> 3 <% if current_user.profile.already_clipped?(@coupon) %> 4 <%= button_to 'クリップ取り消し', clipped_coupon_path(@coupon), method: :delete %> 5 <% else %> 6 <%= button_to 'クリップ', clipped_coupons_path(@coupon) %> 7 <% end %> 8</div>
ruby
1class Profile < ApplicationRecord 2 belongs_to :user 3 has_many :category_profiles 4 has_many :categories, through: :category_profiles 5 has_many :coupon_profiles 6 has_many :coupons, through: :coupon_profiles 7 accepts_nested_attributes_for :category_profiles 8 accepts_nested_attributes_for :coupon_profiles 9 10 def already_clipped?(coupon) 11 self.coupon_profiles.exists?(coupon_id: coupon.id) 12 end
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。