我正在申请录取部分应该有3个步骤,第1步是学生详细信息,第2步是监护人详细信息,第3步是学生以前的数据,第1步和第2步是成功的,问题是当我点击保存并继续
时,我无法达到第三种形式注意:student和student_previous_data之间存在关联 学生has_one student_previous_data并且监护人和学生以前的数据之间没有关系
学生表格将我重定向到监护人表格,监护人表格应该将我重定向到学生以前的数据表格,但它没有
student.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 , :guardians_attributes ,
:student_previous_data_attributes
belongs_to :user
belongs_to :country
belongs_to :school
belongs_to :batch
belongs_to :nationality , class_name: 'Country'
has_many :guardians
has_many :student_previous_subject_marks
has_one :student_previous_data
accepts_nested_attributes_for :guardians
accepts_nested_attributes_for :student_previous_data
end
student_previous_data.rb
class StudentPreviousData < ActiveRecord::Base
attr_accessible :course, :institution, :school_id, :student_id, :total_mark, :year, :student_id
belongs_to :student
end
guardians_controller.rb
class GuardiansController < ApplicationController
def index
@guardian = Guardian.all
end
def show
@guardian = Guardian.find(params[:id])
end
def new
@student = Student.find(params[:student_id])
@guardian = Guardian.new
end
def create
@guardian = Guardian.new(params[:guardian])
if @guardian.save
flash[:success] = ' Parent Record Saved Successfully. Please fill the Additional Details.'
redirect_to controller: 'student_previous_data', action:'new', id: @student.id
else
flash.now[:error] = 'An error occurred please try again!'
render 'new'
end
end
def edit
end
end
student_previous_data_controller.rb
class StudentPreviousDataController < ApplicationController
def index
@student_previous_data = StudentPreviousData.all
end
def show
@student_previous_data = StudentPreviousData.find(params[:id])
end
def new
@student = Student.find(params[:student_id])
@student_previous_data = StudentPreviousData.new
end
def create
@student_previous_data = StudentPreviousData.new(params[:student_previous_data])
if @student_previous_data.save
flash[:success] = 'Record Saved Successfully.'
redirect_to '/user/dashboard'
else
flash.now[:error] = 'An error occurred please try again!'
render 'new'
end
end
def edit
end
end
的routes.rb
get "student_previous_data/index"
get "student_previous_data/show"
get "student_previous_data/edit"
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 :students do
resources :student_previous_data
resources :guardians
end
resources :sessions
get '/user/dashboard', to: 'static_pages#home'
get '/student/admission1' , to: 'students#new'
get '/student/admission2' , to: 'guardians#new'
get '/student/admission3' , to: 'student_previous_data#new'
root to: 'sessions#new'
student_previous_data / new.html.erb
<%= bootstrap_form_for(@student_previous_data, :url => student_student_previous_datum_path(@student),
html: { class: 'form-horizontal' }, method: :post) do |f| %>
<% if @student_previous_data.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@student_previous_data.errors.count, 'error') %>
</div>
<ul>
<% @student_previous_data.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset>
<div class="field">
<%= f.hidden_field :student_id, value: @student.id %>
</div>
<h4>Previous Educational Details</h4>
<div class="field">
<%= f.text_field :institution, label: 'Institution Name'%>
</div>
<div class="field">
<%= f.text_field :course, label: 'Course'%>
</div>
<div class="field">
<%= f.text_field :year, label: 'Year'%>
</div>
<div class="field">
<%= f.text_field :total_mark, label: 'Total Mark'%>
</div>
<div class="actions"><%= f.submit 'Save & Proceed', class: 'btn btn-mini btn-primary' %></div>
</fieldset>
<% end %>
</div>
</div>
我得到的错误是:
RuntimeError in GuardiansController#create
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
app/controllers/guardians_controller.rb:19:in `create'
使用Marek Lipka的解决方案后,我收到以下错误(完全错误):
Routing Error
No route matches {:action=>"show", :controller=>"student_previous_data", :student_id=>#<Student id: 13, admission_no: "587", class_roll_no: nil, admission_date: nil, first_name: "", middle_name: "", last_name: "", date_of_birth: nil, gender: "Male", blood_group: "Unknown", birth_place: "", language: "", religion: nil, address_line1: "", address_line2: "", city: "", post_code: "", phone1: "", phone2: "", email: "", created_at: "2013-06-28 02:12:46", updated_at: "2013-06-28 02:12:46", country_id: nil, user_id: nil, school_id: nil, nationality_id: nil, passport_number: nil, has_paid_fees: nil, enrollment_date: nil, batch_id: nil>}
Try running rake routes for more information on available routes.
在运行rake路线之后我得到了这个:
new_student_student_previous_datum GET /students/:student_id/student_previous_data/new(.:format) student_previous_data#new
也是我的student_previous_data / new.html.erb
<h1>Admission</h1>
<h4>Step 3 - Previous details</h4>
<div class="row-fluid">
<div class="span5 offset1 hero-unit">
<%= bootstrap_form_for(@student_previous_data, :url => student_student_previous_datum_path(@student),
html: { class: 'form-horizontal' }) do |f| %>
<% if @student_previous_data.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@student_previous_data.errors.count, 'error') %>
</div>
<ul>
<% @student_previous_data.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset>
<div class="field">
<%= f.hidden_field :student_id, value: @student.id %>
</div>
<h4>Previous Educational Details</h4>
<div class="field">
<%= f.text_field :institution, label: 'Institution Name'%>
</div>
<div class="field">
<%= f.text_field :course, label: 'Course'%>
</div>
<div class="field">
<%= f.text_field :year, label: 'Year'%>
</div>
<div class="field">
<%= f.text_field :total_mark, label: 'Total Mark'%>
</div>
<div class="actions"><%= f.submit 'Save & Proceed', class: 'btn btn-mini btn-primary' %></div>
</fieldset>
<% end %>
</div>
</div>
那怎么解决这个???
答案 0 :(得分:1)
您应该设置@student
实例变量 - 因为它是nil
而发生错误。您可以在before_filter
中执行此操作,例如:
class GuardiansController < ApplicationController
before_filter :set_student, :only => [:new, :create] # specify in which actions you need this here
# ...
private
def set_student
@student = Student.find params[:student_id]
end
# ..
end
完成此操作后,您可以从已在@student = Student.find(params[:student_id])
中设置的new
操作中删除before_filter
。
此外,在GuardiansController
以及StudentPreviousDataController
中,您的学生由名为student_id
的param代表,因此您的重定向应为:
redirect_to controller: 'student_previous_data', action:'new', student_id: @student.id
答案 1 :(得分:0)
尝试更改此
new_student_student_previous_datum_path(@student)
到
new_student_student_previous_datum_path(student_id: @student.id)
如果它仍然抛出错误,请将完整的路由错误粘贴给我。
更新:
错误在视图中此表单的url当前是
这个产生的错误是:action:“show”
将其更改为
new_student_student_previous_datum_path(@student)
<%= bootstrap_form_for(@student_previous_data, :url => new_student_student_previous_datum_path(@student),
html: { class: 'form-horizontal' }, :method => "post") do |f| %>
新操作和创建操作共享相同的路径,但操作由请求发布或获取
的方法定义