【问题标题】:Rails nested resource params in reverse orderRails以相反的顺序嵌套资源参数
【发布时间】:2021-09-09 03:18:13
【问题描述】:

我花了一段时间尝试调试此行为,但未成功,因此我希望帮助找出为什么我的嵌套资源参数似乎以错误的顺序包含在 URL 中。出于某种原因,可以为课程添加和删除课程,但编辑课程会崩溃,因为 ActiveRecord 尝试使用课程 ID 查找课程,反之亦然。

课程和课程模型

class Course < ApplicationRecord
  has_many :lessons, dependent: :destroy

  extend FriendlyId
  friendly_id :title, use: :slugged

class Lesson < ApplicationRecord
  belongs_to :course

  extend FriendlyId
  friendly_id :title, use: :slugged

课程控制器

class LessonsController < ApplicationController
  before_action :set_lesson, only: [:show, :edit, :update, :destroy ]
  
  def index
    @lessons = Lesson.all
  end

  def show
  end

  def new
    @lesson = Lesson.new
    @course = Course.friendly.find(params[:course_id])
    authorize @lesson
  end

  def edit
    authorize @lesson
  end

  def create
    @lesson = Lesson.new(lesson_params)
    @course = Course.friendly.find(params[:course_id])
    @lesson.course_id = @course.id

    if @lesson.save
      redirect_to course_lesson_path(@course, @lesson), notice: "Lesson was successfully created."
    else
      render :new, status: :unprocessable_entity
    end
  end

  def update
    authorize @lesson
    if @lesson.update(lesson_params)
      redirect_to course_lesson_path(@course, @lesson), notice: "Lesson was successfully updated."
    else
      render :edit
    end
  end

  def destroy
    authorize @lesson
    @lesson.destroy
    redirect_to course_path(@course), notice: "Lesson was successfully destroyed."
  end

  private
    def set_lesson
      @course = Course.friendly.find(params[:course_id])
      @lesson = Lesson.friendly.find(params[:id])
    end

    def lesson_params
      params.require(:lesson).permit(:title, :content, :course_id)
    end
end

路线

  resources :courses do
    resources :lessons
  end

当我做 Rails 路线时会出现什么:

edit_course_lesson GET /courses/:course_id/lessons/:id/edit(.:format)

但是,当我实际编辑课程时,参数似乎会切换,从而导致崩溃。请参阅下面的示例,它认为第四课是课程。

网址:/courses/fourth-lesson/lessons/forensic-science-344/edit

ActiveRecord::RecordNotFound in LessonsController#edit
can't find record with friendly id: "fourth-lesson"
              
  private
    def set_lesson
      @course = Course.friendly.find(params[:course_id]) <- Crashes on this line
      @lesson = Lesson.friendly.find(params[:id])
    end

更新:这是编辑课程的表格。

.container
  = simple_form_for([@course, @lesson]) do |f|
    = f.error_notification
    = f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
  
    .form-inputs
      = f.input :title
      = f.input :content
      %small
      = f.error :content, class: 'text-danger'
  
    .form-actions
      = f.button :submit, class: 'btn btn-primary my-4'

【问题讨论】:

  • 创建 URL 的代码在哪里? (可能是一种形式?)我错过了吗?抱歉,如果我忽略了...
  • 路径切换是在哪里发生的?是编辑表单的链接,表单进行的更新调用,还是保存成功并重定向到显示路径?
  • 我添加了表单的代​​码。至于路径切换的位置,崩溃发生在编辑保存之前。除此之外,不确定如何判断它何时发生 - 关于进一步确定它的任何建议?
  • @Greg 我们需要创建编辑链接的代码。这可能通过传递对象的顺序发生:例如edit_course_lesson_path(lesson, course) 会给出与您得到的类似的错误。正确的方法是:edit_course_lesson_path(course, lesson)

标签: ruby-on-rails nested-routes


【解决方案1】:

原来我没有在编辑链接上传入 course 参数。我有link_to 'Edit', edit_course_lesson_path(lesson)

应该是edit_course_lesson_path(@course, lesson)

感谢大家的耐心等待,非常感谢 Deepesh 终于让我想到了查看编辑链接。

【讨论】:

  • 您可能希望删除该问题,因为它的解决方式不太可能对其他人有帮助,因为这是一个简单的遗漏。无论如何,该问题很可能会被关闭,从而将其从搜索索引中删除。
猜你喜欢
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多