ruby on rails找不到没有ID的学校

时间:2013-08-20 10:30:28

标签: ruby-on-rails

我的项目中有一个has_many关联,基本上我有一个school模型,一个class模型和一个relationships模型

学校模式

class School < ActiveRecord::Base`

  has_many :relationships
  has_many :class_rooms,through: :relationships
  validates :school_id,:presence=>true

end

class_room模型

class ClassRoom < ActiveRecord::Base
  validates :class_id,:class_name,:roll_no,:unique_code,presence:true
  has_many :relationships
  has_many :schools,through: :relationships
end

关系模型

class Relationship < ActiveRecord::Base
  belongs_to :school
  belongs_to :class_room
end

控制器

class SchoolsController < ApplicationController

  def new
    @school=School.new
    @schools=School.find(:all)
  end

  def create
    @school=School.new(params[:school])
    if @school.save
      flash[:sucess]="School saved successfully"
      redirect_to root_url
    else
      render new
    end
  end

  def destroy
    School.find(params[:id]).destroy
    flash[:success]="Deleted successfully"
    redirect_to root_url
  end

end


class ClassRoomsController < ApplicationController
  def new
    @classrooms=ClassRoom.find(:all)
    @school=School.find(params[:school_id])
    @classroom=@school.class_rooms.build
  end

  def create
    @school=School.find(params[:school_id])
    @classroom=@school.class_rooms.build(params[:class_room])
    if @classroom.save
      flash[:sucess]="clasroom saved successfully"
      redirect_to root_url
    else
      render new
    end
  end

end

我的路线文件

Attend::Application.routes.draw do
  root :to => 'schools#new'

  resources :schools do
    resources :class_rooms
  end

  resources :schools
  resources :class_rooms
end

我对学校的新观点

<% provide(:title,'My schools') %>
<div class="row">
 <div class="span6 offset3">
<%=form_for(@school) do |f| %>
 <%render 'shared/error_messages' %>
   <%=f.label :school_id %>
     <%=f.text_field :school_id %>
    <%=f.label :school_name %>
    <%=f.text_field :school_name %>
    <%=f.label :city %>
    <%=f.text_field :city %>
    <%=f.label :state %>
    <%=f.text_field :state %>
    <%=f.submit class:'btn btn-large btn-primary' %>
    <%end%>
    <%=link_to 'Myclass',new_class_room_path %>
   </div>
</div>
<div class="row">
<div class="span6 offset3">
<table border="2">
    <tr>
        <th>Name</th>
        <th>code</th>
        <th>city</th>
        <th>state</th>
        <th></th>
    </tr>
    <% @schools.each do |s| %>
    <tr>
    <td><%=link_to s.school_name,new_class_room_path(:school_id=>s.id) %></td>
            <td><%=s.school_id %></td>
            <td><%=s.city %></td>
            <td><%=s.state %></td>
    <td><%=link_to 'Delete',s,method: :delete,confirm:'Are you sure' %></td>
    </tr>
    <%end%>
</table>
</div>
    </div>

最后是我对class_rooms的新视图

    <% provide(:title,'My schools') %>
     <div class="row">
      <div class="span6 offset3">
   <%=form_for(@classroom) do |f| %>
    <%render 'shared/error_messages' %>
    <%=f.label :class_id %>
    <%=f.text_field :class_id %>
    <%=f.label :class_name %>
    <%=f.text_field :class_name %>
    <%=f.label :rolln_o %>
    <%=f.text_field :roll_no %>
    <%=f.label :unique_code %>
    <%=f.text_field :unique_code %>
    <%=f.submit class:'btn btn-large btn-primary' %>
    <%end%>
      </div>
    </div>

    <div class="row">
<div class="span6 offset3">
<table border="2">
    <tr>
        <th>Name</th>
        <th>code</th>
        <th>rollno</th>
        <th>uniquecode</th>
    </tr>
    <% @classrooms.each do |room| %>
        <tr>
            <td><%=link_to room.class_name%></td>
            <td><%=room.class_id %></td>
            <td><%=room.roll_no %></td>
            <td><%=room.unique_code %></td>
        </tr>
    <%end%>
</table>
</div>
    </div>

现在的问题是new class操作已正确呈现,但是当我点击提交时,它会显示错误Couldn't find School without an ID

2 个答案:

答案 0 :(得分:0)

这是不对的,因为您使用的是form_for(@school)

params[:school_id]

替换为

params[:school][:school_id]

答案 1 :(得分:0)

在学校表单中,无需为school_id添加表单字段,因为在创建School类的新对象时,rails本身会生成唯一的school_id。删除此字段后尝试。