查看Rails记录模态中的关联

时间:2016-08-11 18:28:45

标签: ruby-on-rails twitter-bootstrap

在我的Rails应用中,我有3个模型KidClassroomTeacher。基本上,我在视图中有一个表格,其中列出了所有教室,当我点击一个按钮时,我想要一个弹出模式,列出教师学生被分配到该课堂,因为有Classroom has_many Teachers和has_many Kids的关联。我已经为模式准备好了代码,我已将其复制到下面,但是我很难将特定的教室对象确定为模态。所有帮助表示赞赏!

<div class="modal fade" id="showClassModal" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 id="showClassroomHeader"></h4>
      </div>
      <div class="modal-body">  
        <h4>Teachers Assigned</h4>
         <ol>
          <% @classroom.teachers.each do |teacher| %>
            <li><%= teacher.first_name %> <%= teacher.last_name %></li>
          <% end %>          
         </ol>
         <br>
        <h4>Students</h4>
         <ol>
          <% @classroom.kids.each do |kid| %>
           <li><%= kid.first_name %> <%= kid.last_name %></li>
          <% end %>          
        </ol>    
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <% if current_user.admin? %>
          <button class="btn btn-warning" id="editClassroomBtn">Edit</button>
        <% end %>
      </div>
    </div>
  </div>

修改

课堂控制器:

class ClassroomsController < ApplicationController
  before_action :set_classroom, only: [:show, :edit, :update, :destroy]

 # GET /classrooms
 # GET /classrooms.json
  def index
    @user ||= current_user
    if current_user.admin?
      @classrooms = Classroom.all
    else
      @classrooms = @user.classrooms.all
    end  
  end

  # GET /classrooms/1
  # GET /classrooms/1.json
  def show
    classroom = Classroom.find(params[:id])
    render :text => classroom.class_desc
  end

  # GET /classrooms/new
  def new
    @classroom = Classroom.new
  end

  # GET /classrooms/1/edit
  def edit
  end

  # POST /classrooms
  # POST /classrooms.json
  def create
    @classroom = Classroom.new(classroom_params)
    classroom.user = current_user
    respond_to do |format|
       if @classroom.save
        format.html { redirect_to root_url, notice: 'Classroom was successfully created.' }
        format.json { render :show, status: :created, location: @classroom }
      else
        format.html { render :new }
        format.json { render json: @classroom.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /classrooms/1
  # PATCH/PUT /classrooms/1.json
  def update
    respond_to do |format|
      if @classroom.update(classroom_params)
        format.html { redirect_to root_url, notice: 'Classroom was successfully updated.' }
        format.json { render :show, status: :ok, location: @classroom }
  else
        format.html { render :edit }
        format.json { render json: @classroom.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /classrooms/1
  # DELETE /classrooms/1.json
  def destroy
    @classroom.destroy
    respond_to do |format|
      format.html { redirect_to classrooms_url, notice: 'Classroom was successfully destroyed.' }
      format.json { head :no_content }
    end
 end

  private
   # Use callbacks to share common setup or constraints between actions.
   def set_classroom
    @classroom = Classroom.find(params[:id])
   end

    # Never trust parameters from the scary internet, only allow the white list through.
    def classroom_params
  params.require(:classroom).permit(:class_name, :class_desc, :capacity, :start_range, :end_range)
    end
end 

routes.rb中:

Rails.application.routes.draw do
  resources :rationales
  resources :teachers
  resources :classrooms
  resources :kids
  devise_for :users, :skip => [:sessions]

  as :user do
    get 'login' => "devise/sessions#new", :as => :new_user_session
    post 'login' => 'devise/sessions#create', :as => :user_session
    delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
  end

  root :to => 'kids#index'



 resources :kids do
  get 'discharge', on: :member
 end

 resources :kids do
   get 'restore', on: :member
 end


end

1 个答案:

答案 0 :(得分:0)

正如上面评论部分所述,在<% @classrooms.each do |classroom| %>中添加模态就可以了!