我的项目中有一个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