Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

ruby on rails - Cant reach to the next form 'RoR 3+'

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should set @student instance variable - the error occurs because it is nil. You can do it in before_filter, for example:

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

When this is done, you can remove @student = Student.find(params[:student_id]) from your new action as it is already set in before_filter.

What's more, in your GuardiansController as well as StudentPreviousDataController, you have student represented by param named student_id, so your redirection should be:

redirect_to controller: 'student_previous_data', action:'new', student_id: @student.id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...