如何将值传递给自定义外键并使外键在第二种形式中显示其值

时间:2013-06-25 11:38:37

标签: ruby-on-rails ruby-on-rails-3 model form-for

如何将值传递给自定义外键(ward_id)以及如何使将以学生形式插入的录取号码出现在第二种形式的父/监护人中> ward_id,我有以下几点: 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.'
      redirect_to new_guardian_url
    else
      flash.now[:error] = 'An error occurred please try again!'
      render 'new'
    end
  end

  def edit
  end
end

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
  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

guardians_controller.rb

class GuardiansController < ApplicationController
  def index
  end

  def show
  end

  def new
    @guardian = Guardian.new
  end

  def edit
  end
end

guardian.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

的routes.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

0 个答案:

没有答案