我正在创建一个表格,用于在第一步中提交学生详细信息,在第二步中应提交家长(监护人)详细信息。我为学生创建了一个控制器,为监护人创建了另一个控制器,填写详细信息后,我需要在第一步按下提交按钮时,应该出现第二步,即家长/监护人详细信息,但我在实现这一点时遇到了问题。我的文件是:students_controller.rb
class StudentsController < ApplicationController
def index
@student = Student.all
end
def show
@student = Student.find(params[:id])
end
def new
@student = Student.new
end
def create
@student = Student.new(params[:student])
if @student.save
flash[:success] = ' Student Record Saved Successfully. Please fill the Parent Details.'
render 'guardians/new'
else
flash.now[:error] = 'An error occurred please try again!'
render 'new'
end
end
def edit
end
end
学生.rb
class Student < ActiveRecord::Base
attr_accessible :address_line1, :address_line2, :admission_date, :admission_no, :birth_place, :blood_group, :city,
:class_roll_no, :date_of_birth, :email, :first_name, :gender, :language, :last_name, :middle_name,
:phone1, :phone2, :post_code, :religion, :country_id, :nationality_id
belongs_to :user
belongs_to :country
belongs_to :school
belongs_to :batch
belongs_to :nationality , class_name: 'Country'
has_many :guardians , foreign_key: 'ward_id'
has_many :student_previous_subject_marks
has_one :student_previous_data
end
监护人控制器.rb
class GuardiansController < ApplicationController
def index
end
def show
end
def new
@guardian = Guardian.new
end
def edit
end
end
监护人.rb
class Guardian < ActiveRecord::Base
attr_accessible :city, :dob, :education, :email, :first_name, :income, :last_name, :mobile_phone, :occupation,
:office_address_line1, :office_address_line2, :office_phone1, :office_phone2, :relation
belongs_to :user
belongs_to :country
belongs_to :school
belongs_to :ward , class_name: 'Student'
end
路线.rb
Triton::Application.routes.draw do
get "guardians/index"
get "guardians/show"
get "guardians/edit"
get "students/index"
get "students/show"
get "students/edit"
resources :articles do
resources :comments
end
resources :users
resources :guardians
resources :students
resources :sessions
get '/user/dashboard', to: 'static_pages#home'
get '/student/admission1' , to: 'students#new'
get '/student/admission2' , to: 'guardians#new'
root to: 'sessions#new'
end
学生/new.html.erb
<h1>Admission</h1>
<h4>Step 1 - Student details</h4>
<div>
<div class="span6 offset2">
<%= form_for(@student) do |f| %>
<% if @student.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@student.errors.count, 'error') %>
</div>
<ul>
<% @student.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<table border="0" style="width: 65%" class='hero-unit'>
<tr>
<th><div class="field"><%= f.label :admission_no %></div></th>
<th><div class="field"><%= f.text_field :admission_no %></div></th>
</tr>
<tr>
<th><div class="field"><%= f.label :admission_date %></div></th>
<th><div class="field"><%= f.text_field :admission_date , :class => 'datepicker'%></div></th>
</tr></table>
<h4>Student - Personal Details</h4>
<table border="0" style="width: 65%" class='hero-unit'>
<tr>
<th><div class="field"><%= f.label :first_name %></div></th>
<th><div class="field"><%= f.text_field :first_name %></div></th>
</tr>
<tr>
监护人/new.html.erb
<h1>Admission</h1>
<h4>Step 2 - Parent details</h4>
<div class="row-fluid">
<div class="span4 offset1 hero-unit">
<%= form_for @guardian do |f| %>
<% if @guardian.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@guardian.errors.count, 'error') %>
</div>
<ul>
<% @guardian.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset>
<div class="field">
<%= f.label 'Student Admission number' %>
<%= f.text_field :ward_id , disabled: true %>
</div>
<h4>Parent - Personal Details</h4>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
//not the whole file, just copied what is needed to be seen
我面临的错误
NoMethodError in Students#create
undefined method `model_name' for NilClass:Class
Extracted source (around line #45):
42:
43: <div class="row-fluid">
44: <div class="span4 offset1 hero-unit">
45: <%= form_for @guardian do |f| %>
46: <% if @guardian.errors.any? %>
47: <div id="error_explanation">
48: <div class="alert alert-error">
我希望你能明白我的意思来解决我的问题,谢谢!