在 Rails 中创建嵌套资源时没有路由匹配

No route matches while creating nested resource in Rails

我正在尝试为我的项目中的特定学校创建新教师,但出现此错误:

No route matches [POST] "/schools/3/teachers/new"

这是我的 teachers_controller.rb:

class TeachersController < ApplicationController
  def new
    @teacher = Teacher.new
  end

  def create
    @teacher = Teacher.new(teacher_params)
    @teacher.save

    redirect_to school_path(school)
  end

  private

    def teacher_params
      params.require(:teacher).permit(:firstName, :lastName, :middleName)
    end
end

schools_controller.rb:

class SchoolsController < ApplicationController
  def show
    @school = School.find(params[:id])
  end

  def new
    @school = School.new
  end

  def edit
    @school = School.find(params[:id])
  end

  def update
    @school = School.find(params[:id])

    if @school.update(school_params)
      redirect_to @school
    else
      render 'edit'
    end
  end

  def index
    @schools = School.all
  end

  def create
    @school = School.new(school_params)

    if @school.save
      redirect_to schools_path
    else
      render 'new'
    end
  end

  def destroy
    @school = School.find(params[:id])
    @school.destroy

    redirect_to schools_path
  end

  private

    def school_params
      params.require(:school).permit(:name)
    end
end

routes.rb:

Rails.application.routes.draw do
  resources :schools do
    resources :teachers
  end

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'welcome#index'

和teachers/new.html.erb:

<%= form_for :teacher, url: school_teachers_path(school) do |f| %>
  <p>
    <%= f.label :firstName %><br>
    <%= f.text_field :firstName %>
  </p>

  <p>
    <%= f.label :lastName %><br>
    <%= f.text_field :lastName %>
  </p>

  <p>
    <%= f.label :middleName %><br>
    <%= f.text_field :middleName %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

在您的表单中试试这个:

<%= form_for [school, Teacher.new] do |f| %>

您发布的路径是学校教师索引:

school_teachers GET    /schools/:school_id/teachers(.:format)   teachers#index

由于您的teacher资源嵌套在school资源下,所以您在尝试创建教师时需要通过学校。

尝试将 teachers_controller.rb 中的 newcreate 操作更改为如下内容:

  def new
    @school = School.find(params[:school_id])
    @teacher = @school.teachers.build
  end

  def create
    @school = School.find(params[:school_id])
    @teacher = @school.teachers.build(params[:teacher])
    @teacher.save
    redirect_to school_path(@school)
  end

然后,将您的表单更改为:

<%= form_for([@school, @teacher]) do %>
. . .
. . .
<% end %>

我认为这是一个 has_many belongs_to 协会。所以你需要先改变你的教师控制器创建动作和新动作。

class TeachersController < ApplicationController
 def new
  get_school
  @teacher = @school.teachers.build
 end

 def create
  get_school
  @teacher = @school.teachers.build(teacher_params)
  If @teacher.save
      redirect_to school_path(school)
  else
      render 'new'
  end

 private

  def teacher_params
    params.require(:teacher).permit(:firstName, :lastName, :middleName)
   end
 def get_school
     @school = School.find (params [:school_id])
 end
 end

然后在你的表格中你会做:

  <%= form_for([@school,@teacher]) do |f| %>

希望这会有所帮助