現在courses, reviewsという二つのテーブルがあり、courses/formからreviewsテーブルの値も受け取り、テーブルに保存しようとしています。UIの部分は出来たのですが、course/form から入力されたreviews_contentの値を保存することができていない状況です。Schema, controllerなどの構成は以下の通りです。@course.reviews.build、nested_attributesなども試してみましたが、保存ができていないようです。consoleからは無事にreviewsテーブルの保存ができます。
rails
1ActiveRecord::Schema.define(version: 2019_12_10_014132) do 2 3 create_table "courses", force: :cascade do |t| 4 t.string "coursename" 5 t.text "course_content" 6 t.datetime "created_at", null: false 7 t.datetime "updated_at", null: false 8 t.string "prerequisite" 9 t.string "proctoredexams" 10 t.string "groupwork" 11 t.string "textbook" 12 end 13 14 create_table "reviews", force: :cascade do |t| 15 t.text "reviews_content" 16 t.datetime "created_at", null: false 17 t.datetime "updated_at", null: false 18 t.integer "course_id" 19 end 20 21end
rails
1class CoursesController < ApplicationController 2 before_action :set_course, only: [:edit, :update, :show, :destroy] 3 4 def index 5 @courses = Course.all 6 end 7 8 def new 9 @course = Course.new 10 11 #@course = Course.first 12 # for nested_attributes 13 @course.reviews.build 14 end 15 16 def create 17 @course = Course.new(course_params) 18 @course.save 19 20 if @course.save 21 flash[:success] = "New course has created" 22 redirect_to course_path(@course) 23 else 24 render 'new' 25 end 26 end 27 28 def show 29 #@course = Course.find(params[:id]) 30 #@courses = Course.all 31 end 32 33 def destroy 34 @course = Course.find(params[:id]) 35 @course.destroy 36 37 flash[:danger] = "Course was deleted" 38 redirect_to courses_path 39 end 40 41 def edit 42 #@course = Course.find(params[:id]) 43 end 44 45 def update 46 #@course = Course.find(params[:id]) 47 if @course.update(course_params) 48 flash[:success] = "Course was updated with reviews" 49 redirect_to course_path(@course) 50 else 51 render 'edit' 52 end 53 end 54 55 private 56 def set_course 57 @course = Course.find(params[:id]) 58 end 59 60 def course_params 61 params.require(:course).permit(:coursename, :course_content, :prerequisite, :proctoredexams, :groupwork, :textbook, reviews_attributes: [:reviews_content]) 62 end 63end
rails
1<div class = "row"> 2 <div class = "col-xs-12"> 3 <%= form_with( model: @course, local: true, class: "form-horizontal") do |f| %> 4 <div class = "form-group"> 5 <div class = "control-label col-sm-2"> 6 <%= f.label :coursename %> 7 </div> 8 9 <div class = "col-sm-8"> 10 <%= f.text_field :coursename, class: "form-control", placeholder: "Name of class", autofocus: true %> 11 </div> 12 </div> 13 14 <div class = "form-group"> 15 <div class = "control-label col-sm-2"> 16 <%= f.label :cocurse_content %> 17 </div> 18 19 <div class = "col-sm-8"> 20 <%= f.text_field :course_content, class: "form-control", placeholder: "Description of course", autofocus: true %> 21 </div> 22 </div> 23 24 <div class="form-group"> 25 <div class = "control-label col-sm-2"> 26 <%= f.label :reviews %> 27 </div> 28 <div class = "col-sm-8"> 29 <%= f.text_field :reviews_content, class: "form-control", placeholder: "Leave the reviews", autofocus: true, id: :course_id %> 30 </div> 31 </div> 32 33 <div class = "form-group"> 34 <div class = "col-sm-offset-2 col-sm-10"> 35 <%= f.submit class: 'btn btn-primary btn-lg' %> 36 </div> 37 </div> 38 39
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。