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

时间:2015-11-16 05:29:51

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2 nested-attributes

我正在尝试为我的项目中的特定学校创建一名新老师,我收到了这个错误:

  

没有路线匹配[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'

老师/ 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 %>

3 个答案:

答案 0 :(得分:2)

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

尝试将new中的createteachers_controller.rb操作更改为以下内容:

  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 %>

答案 1 :(得分:1)

我相信这是一个has_many belongs_to协会。因此,您需要先更改教师控制器创建操作和新操作。

public class Tokenizer
{
  public static void main(String[] args)
  {
    int index = 0; int tokenCount;
    String words[] = new String [50];
    String message="The Quick brown fox the";

    StringTokenizer string = new StringTokenizer(message);

    tokenCount = string.countTokens();
    System.out.println("Number of tokens = " + tokenCount);
    while (string.hasMoreTokens()) 
        { words[index] = string.nextToken(); index++; }
    for (index=0;index<tokenCount; index++)
        { System.out.println(words[index]); }
  }
}

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

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

希望这会有所帮助

答案 2 :(得分:0)

在表单中尝试:

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

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

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