オリジナルアプリで商品のレビューページを作成中ですが、アソシエーションで関連づけたusers.idとproduct.idをnewページからcreateした時に、うまくDBに格納できません。privateメソッドにmargeで追加してみたりしましたが引数の渡し方に問題があるのか、うまく格納できません。ご教授頂けたらと思います。
user model
has_many :products, dependent: :destroy has_many :reviews, dependent: :destroy
product model
belongs_to :user has_many :reviews, dependent: :destroy
review model
belongs_to :user belongs_to :product
reviews_controller
def new @product = Product.find(params[:id]) @review = Review.new end def create review = Review.new(review_params) if review.save redirect_to products_path, notice: "保存が成功しました" else render action: :new, notice: "保存が失敗しました" end end private def review_params params.require(:review).permit(:total, :package, :price, :color, :amount, :naming, :concept, :balance, :comment).merge(user_id: @current_user.id, product_id: params[:product_id]) end
new.html.erb
<h1>Review sheets</h1> <%= flash[:notice] %> <%= form_with model: @product,@review do |f| %> <div><%= f.label :'総合' %><div> <div><%= f.radio_button :total, "1" %> 1<div> <div><%= f.radio_button :total, "2" %> 2<div> <div><%= f.radio_button :total, "3" %> 3<div> <div><%= f.radio_button :total, "4" %> 4<div> <div><%= f.radio_button :total, "5" %> 5<div> <div><%= f.label :'パッケージ' %><div> <div><%= f.radio_button :package, "1" %> 1<div> <div><%= f.radio_button :package, "2" %> 2<div> <div><%= f.radio_button :package, "3" %> 3<div> <div><%= f.radio_button :package, "4" %> 4<div> <div><%= f.radio_button :package, "5" %> 5<div> <div><%= f.label :'価格' %><div> <div><%= f.radio_button :price, "1" %> 1<div> <div><%= f.radio_button :price, "2" %> 2<div> <div><%= f.radio_button :price, "3" %> 3<div> <div><%= f.radio_button :price, "4" %> 4<div> <div><%= f.radio_button :price, "5" %> 5<div> <div><%= f.label :'カラー' %><div> <div><%= f.radio_button :color, "1" %> 1<div> <div><%= f.radio_button :color, "2" %> 2<div> <div><%= f.radio_button :color, "3" %> 3<div> <div><%= f.radio_button :color, "4" %> 4<div> <div><%= f.radio_button :color, "5" %> 5<div> <div><%= f.label :'量・大きさ' %><div> <div><%= f.radio_button :amount, "1" %> 1<div> <div><%= f.radio_button :amount, "2" %> 2<div> <div><%= f.radio_button :amount, "3" %> 3<div> <div><%= f.radio_button :amount, "4" %> 4<div> <div><%= f.radio_button :amount, "5" %> 5<div> <div><%= f.label :'ネーミング' %><div> <div><%= f.radio_button :naming, "1" %> 1<div> <div><%= f.radio_button :naming, "2" %> 2<div> <div><%= f.radio_button :naming, "3" %> 3<div> <div><%= f.radio_button :naming, "4" %> 4<div> <div><%= f.radio_button :naming, "5" %> 5<div> <div><%= f.label :'コンセプト' %><div> <div><%= f.radio_button :concept, "1" %> 1<div> <div><%= f.radio_button :concept, "2" %> 2<div> <div><%= f.radio_button :concept, "3" %> 3<div> <div><%= f.radio_button :concept, "4" %> 4<div> <div><%= f.radio_button :concept, "5" %> 5<div> <div><%= f.label :'バランス' %><div> <div><%= f.radio_button :balance, "1" %> 1<div> <div><%= f.radio_button :balance, "2" %> 2<div> <div><%= f.radio_button :balance, "3" %> 3<div> <div><%= f.radio_button :balance, "4" %> 4<div> <div><%= f.radio_button :balance, "5" %> 5<div> <div><%= f.label :'コメント' %></div> <div><%= f.text_field :comment %></div> <div><%= f.hidden_field_tag :product_id, @product.id %></div> <div><%= f.submit "レビューする" %></div> <% end %> <div><%= link_to "トップページ", products_path %></div>
エラーコード Started POST "/reviews" for ::1 at 2020-05-21 11:01:42 +0900 Processing by ReviewsController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"HiH793Sir412NVnz6Gs9R6/AIPH0nwLZG/MHrTuntsZn8xvcwEAMbqY9jLNAPo6sSeDfPk8AidgHahkG9DrFtg==", "review"=>{"total"=>"1", "package"=>"3", "price"=>"2", "color"=>"2", "amount"=>"1", "naming"=>"5", "concept"=>"5", "balance"=>"5", "comment"=>"aaaaaaaaa"}, "commit"=>"レビューする"} User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 ↳ app/controllers/application_controller.rb:4 (0.4ms) BEGIN ↳ app/controllers/reviews_controller.rb:16 CACHE User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]] ↳ app/controllers/reviews_controller.rb:16 (0.5ms) ROLLBACK ↳ app/controllers/reviews_controller.rb:16 Rendering reviews/new.html.erb within layouts/application Rendered reviews/new.html.erb within layouts/application (17.2ms) Completed 200 OK in 82ms (Views: 69.7ms | ActiveRecord: 1.4ms)
ログ ActionView::Template::Error (undefined method `id' for nil:NilClass): 54: <div><%= f.submit "レビューする" %></div> 55: <% end %> 56: <div><%= link_to "トップページ", products_path %></div> 57: <%= hidden_field_tag :product_id, @product.id %> app/views/reviews/new.html.erb:57:in `_app_views_reviews_new_html_erb___2040207276415451298_70112480660040' app/controllers/reviews_controller.rb:20:in `create'
review.save!にした時のログ
app/views/reviews/new.html.erb:57:in _app_views_reviews_new_html_erb___2040207276415451298_70112480660040' app/controllers/reviews_controller.rb:20:in
create'
Started POST "/reviews" for ::1 at 2020-05-21 22:08:48 +0900
(0.2ms) 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
↳ /Users/mami.igarashi/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98
Processing by ReviewsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HiH793Sir412NVnz6Gs9R6/AIPH0nwLZG/MHrTuntsZn8xvcwEAMbqY9jLNAPo6sSeDfPk8AidgHahkG9DrFtg==", "review"=>{"total"=>"5", "package"=>"3", "price"=>"2", "color"=>"2", "amount"=>"1", "naming"=>"5", "concept"=>"5", "balance"=>"5", "comment"=>"aaaaaaaaa"}, "commit"=>"レビューする"}
User Load (0.3ms) SELECT users
.* FROM users
WHERE users
.id
= 2 LIMIT 1
↳ app/controllers/application_controller.rb:4
(0.2ms) BEGIN
↳ app/controllers/reviews_controller.rb:17
CACHE User Load (0.0ms) SELECT users
.* FROM users
WHERE users
.id
= 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/reviews_controller.rb:17
(0.2ms) ROLLBACK
↳ app/controllers/reviews_controller.rb:17
Completed 422 Unprocessable Entity in 54ms (ActiveRecord: 6.8ms)
ActiveRecord::RecordInvalid (Validation failed: Product must exist, Product can't be blank):
app/controllers/reviews_controller.rb:17:in `create'
application_controller
class ApplicationController < ActionController::Base
before_action :set_current_user
def set_current_user
@current_user = User.find_by(id: session[:user_id])
end
end