我的项目中有一个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 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>
<% 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
答案 0 :(得分:0)
这是不对的,因为您使用的是form_for(@school)
params[:school_id]
替换为
params[:school][:school_id]
答案 1 :(得分:0)
在学校表单中,无需为school_id
添加表单字段,因为在创建School类的新对象时,rails本身会生成唯一的school_id
。删除此字段后尝试。