当我去创建教学大纲时回滚

rollback when I go Create a Syllabus

我可以通过终端制作 CRUD 教学大纲,但是当我通过我的程序界面进行制作时,它不允许我“创建”并进行回滚。如何调试这个?我已经尝试了很多方法,我不知道到底发生了什么。最奇怪的是我总是设法通过终端来做到这一点。

我的控制器

    before_action :set_syllabus, only: [:show, :update, :destroy]

    def show
      if @syllabus
        scheduled_dates = JSON.parse @syllabus.scheduled_dates
        # interests_array = @syllabus.student_interests.pluck(:data)
        
        if current_student.status != "freemium"
          render json: {
            goal: @syllabus.goal,
            objective: @syllabus.objective,
            level: @syllabus.level,
            estimate: @syllabus.estimate,
            interests: @syllabus.interests,
            skills: @syllabus.skills,
            schedule: {
                dates: scheduled_dates,
                minutes: @syllabus.scheduled_minutes
            }
            # }.merge({:interests => interests_array })
          }
        else
          render json: {
            blocked: true,
            goal: @syllabus.goal,
            objective: @syllabus.objective,
            level: @syllabus.level,
            estimate: @syllabus.estimate,
            interests: @syllabus.interests,
            skills: @syllabus.skills,
            schedule: {
                dates: scheduled_dates,
                minutes: @syllabus.scheduled_minutes
            }
            # }.merge({:interests => interests_array })
          }
        end
      else
        render json: {
          errors: "Record not found."
        }, status: 404
      end
    end

    def create
      @syllabus = Syllabus.new(syllabus_params)

      if @syllabus.save
        render json: @syllabus.as_json, status: :created
      else
        render json: {
            errors: @syllabus.errors
          }, status: :unprocessable_entity
      end
    end

    def update
      if @syllabus.update(syllabus_params)
        render json: @syllabus.as_json(except: [:id]), status: :ok
      else
        render json: {
          errors: @syllabus.errors
        }, status: :unprocessable_entity
      end
    end

    def destroy
      if @syllabus.destroy
        render :no_content
      else       
        render json: { errors: 
        breweries.errors.full_messages 
        }, status: :unprocessable_entity
      end
    end
  
    private
  
      def set_syllabus
        @syllabus = Syllabus.find_by(student_id: params[:id])
      end



      def syllabus_params
        params.permit(
          :student_id,
          :objective,
          :level,
          :goal,
          :estimate,
          :interests,
          :skills,
          :scheduled_dates,
          :scheduled_minutes
        )
      end
  end```



```[619aec90-7658-4825-aebb-82fc7aecc978] Started POST "/api/v1/students/syllabus" for ::1 to 2021-08-05 14:35:52 -0300
[619aec90-7658-4825-aebb-82fc7aecc978] Processing by Api::V1::SyllabusController#create as HTML
[619aec90-7658-4825-aebb-82fc7aecc978] Parameters: {"student_id"=>52776, "level"=>"Basic", "objective"=>"Be able to travel more smoothly", "goal"=>"Advanced ", "estimate"=>14.625, "scheduled_dates"=>["dom", "sab"], "scheduled_minutes"=>200, "interests"=>["Sports", ""], "syllabus"=> {"student_id"=>52776, "objective"=>"Be able to travel more smoothly", "level"=>"Basic", "goal"=>"Advanced", "estimate"=>14.625, "scheduled_dates"= >["dom", "sat"], "scheduled_minutes"=>200, "interests"=>["Sports", ""]}}
[619aec90-7658-4825-aebb-82fc7aecc978] Unpermitted parameters: :scheduled_dates, :interests, :syllabus
[619aec90-7658-4825-aebb-82fc7aecc978] (0.4ms) BEGIN
[619aec90-7658-4825-aebb-82fc7aecc978] ↳ app/controllers/api/v1/syllabus_controller.rb:49
[619aec90-7658-4825-aebb-82fc7aecc978] Student Load (0.5ms) SELECT "students".* FROM "students" WHERE "students"."id" =  LIMIT  [["id", 52776], [" LIMIT", 1]]
[619aec90-7658-4825-aebb-82fc7aecc978] ↳ app/controllers/api/v1/syllabus_controller.rb:49
[619aec90-7658-4825-aebb-82fc7aecc978] (0.2ms) ROLLBACK
[619aec90-7658-4825-aebb-82fc7aecc978] ↳ app/controllers/api/v1/syllabus_controller.rb:49
[619aec90-7658-4825-aebb-82fc7aecc978] Completed 422 Unprocessable Entity in 541ms (Views: 0.1ms | ActiveRecord: 5.2ms)```

错误就在那里,unpermitted parameters。 您需要将 require(:syllabus) 添加到强参数中。

params.require(:syllabus).permit(
          :student_id,
          :objective,
          :level,
          :goal,
          :estimate,
          :skills,
          :scheduled_minutes
          interests: [],
          scheduled_dates: [],
        )