In my Rails app I have 3 models Kid
, Classroom
, and Teacher
. Basically, I have a table in my view which lists all the classrooms , when I click on a button I want a modal to pop up that lists the teachers and students assigned to that classroom, as there is an association where a Classroom
has_many Teachers
and has_many Kids
. I have the code ready for the modal, I have copied it below, however I am having a difficult time scoping a specific classroom object to the modal. All help is appreciated!
<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">×</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>
EDIT:
Classroom controller:
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
As mentioned in the comments section above, adding the modal inside <% @classrooms.each do |classroom| %>
<% @classrooms.each do |classroom| %>
did the trick!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.