I am making an admission section in my application which should be have 3 steps , 1st step the the student details , 2nd step is the guardian details , 3rd step is the student previous data , step 1 and 2 was made successfully , the problem is I cant reach to the third form when I click Save and proceed
NOTE: there is a relation between student and student_previous_data
student has_one student_previous_data and there is no relationship between guardians and student previous data
student form redirects me to guardians form , and guardians form should redirects me to student previous data form but it doesn't
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>
the error I am getting is:
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'
after using Marek Lipka's Solution I got the following error (FULL ERROR):
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.
and after running rake routes i got this:
new_student_student_previous_datum GET /students/:student_id/student_previous_data/new(.:format) student_previous_data#new
and Also that is my 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>
SO how to fix this ???
See Question&Answers more detail:
os