実現したい事
スポーツのレビューサイトを作成したい。
レビュー投稿時にはDBにある試合の中からどの試合に対してのレビューを書くか、ユーザーが選択できるようにする。
現状
自分なりに調べてcollection_selectを記述してみたが、view上に反映されない。(エラーは起きないが何も出てこない)
実装内容
テーブルのカラム
reviewsテーブルのカラム
id、user_id、match_id、text
matchesテーブルのカラム
id、date、rival_team
モデル(アソシエーション)
models/review.rb
ruby
1belongs_to :user 2belongs_to :match
models/match.rb
ruby
1has_many :reviews
コントローラ
controllers/reviews_controller.rb
ruby
1def new 2 @review = Review.new 3end 4 5def create 6 Review.create(review_params) 7 redirect_to_index 8end 9 10private 11def review_params 12 params.require(:review).permit(:title, :text, :match_id).merge(user_id: current_user.id) 13end
ビュー
= form_with(model: @review, local:true) do |f| .row .col.s8.offset-s2 = f.label :"対戦チーム" = f.collection_select :match_id, Match.all, :rival_country, :id = f.label :"レビュー" = f.text_area :text, class: "materialize-textarea" = f.submit "送信"
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/05 09:17
2019/08/05 11:36
2019/08/05 12:36