-2

我在我的申请中做一个录取部分,应该有 3 个步骤,第 1 步是学生详细信息,第 2 步是监护人详细信息,第 3 步是学生以前的数据,第 1 步和第 2 步都成功了,问题是我当我单击保存并继续时,无法访问第三个表单

注意:student 和 student_previous_data student 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

监护人控制器.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

路线.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 的解决方案后,出现以下错误(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. 

在运行 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>

那么如何解决这个问题???

4

2 回答 2

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.

更重要的是,在您的GuardiansControlleras 中StudentPreviousDataController,您有由参数命名的 student 表示student_id,因此您的重定向应该是:

redirect_to controller: 'student_previous_data', action:'new', student_id: @student.id
于 2013-06-27T22:11:24.480 回答
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| %>

new 和 create 操作共享相同的路径,但该操作由请求postget的方法定义

于 2013-06-28T02:00:54.107 回答