在rails上显示来自连接表ruby的数据

时间:2016-12-06 11:29:57

标签: ruby-on-rails ruby ruby-on-rails-4

我试图在提交表上显示学生姓名,但我不知道该怎么做。如果你能帮助我!

Form.rb:

class Form < ActiveRecord::Base
 belongs_to :user
 has_many :submissions, :dependent => :destroy

 has_attached_file :image, styles: { medium: "400x600>" }
 validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

Submission.rb:

class Submission < ActiveRecord::Base
 belongs_to :form

 has_many :submissionstudent
 has_many :students, :through => :submissionstudent
end

Student.rb:

class Student < ActiveRecord::Base
 has_many :submissionstudent
 has_many :submissions, :through => :submissionstudent
end

联合表:Submissionstudent:

class Submissionstudent < ActiveRecord::Base
 belongs_to :submission
 belongs_to :student
end

显示表格:

<h1><%= @form.title %></h1>
<p>
  <%= image_tag @form.image.url(:medium) %>
</p>

<table class="table table-responsive table-hover">
  <% if user_signed_in? %>
    <% if @submissions.blank? %>
      <h4>No submission just yet</h4>
    <% else %>
    <thead>
      <th>Conflict</th>
      <th>Computer</th>
      <th>Extra time</th>
      <th>AM or PM</th>
    </thead>

    <tbody>
      <% @submissions.each do |submission| %>
        <tr>
          <td><%= submission.conflict %></td>
          <td><%= submission.computer %></td>
          <td><%= submission.extra_time %>%</td>
          <td><%= submission.am_pm %></td>
          <!-- Need to add Edit, Delete -->
        </tr>
      <% end %>

    </tbody>
    <% end %>
  <% end %>
</table>

<%= link_to 'New Submission', new_form_submission_path(@form) %>
<br>
<%= link_to 'Edit', edit_form_path(@form) %> |
<%= link_to 'Back', forms_path(@form) %>

提交控制器:

class SubmissionsController < ApplicationController
  before_action :set_submission, only: [:show, :edit, :update, :destroy]
  before_action :set_form

  # GET /submissions/new
  def new
    @submission = Submission.new

    @all_students = Student.all

    @submission_student = @submission.submissionstudent.build
  end

  # GET /submissions/1/edit
  def edit
  end

  # POST /submissions
  # POST /submissions.json
  def create
    @submission = Submission.new(submission_params)
    @submission.form_id = @form.id

    respond_to do |format|
      if @submission.save
        format.html { redirect_to @form, notice: 'Submission was successfully created.' }
        format.json { render :show, status: :created, location: @submission }
      else
        format.html { render :new }
        format.json { render json: @submission.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /submissions/1
  # DELETE /submissions/1.json
  def destroy
    @submission.destroy
    respond_to do |format|
      format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    def set_form
      @form = Form.find(params[:form_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def submission_params
      params.require(:submission).permit(:conflict, :computer, :extra_time, :am_pm)
    end
end

表单控制器:

class FormsController < ApplicationController


before_action :set_form, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]


  def index
    @forms = Form.all
  end

  def show
    @submissions = Submission.where(form_id: @form.id).order("conflict DESC")
    @student = Student.find params[:id]
  end

  def new
    @form = current_user.forms.build
  end

  def edit
  end

  def create
    @form = current_user.forms.build(form_params)

    respond_to do |format|
      if @form.save
        format.html { redirect_to @form, notice: 'Form was successfully created.' }
        format.json { render :show, status: :created, location: @form }
      else
        format.html { render :new }
        format.json { render json: @form.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /forms/1
  # DELETE /forms/1.json
  def destroy
    @form.destroy
    respond_to do |format|
      format.html { redirect_to forms_url, notice: 'Form was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def form_params
      params.require(:form).permit(:title, :image)
    end
end

学生管理员:

class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]

  # GET /students
  # GET /students.json
  def index
    @students = Student.all
  end

  # GET /students/1
  # GET /students/1.json
  def show
  end

  # GET /students/new
  def new
    @student = Student.new
  end

  # GET /students/1/edit
  def edit
  end

  # POST /students
  # POST /students.json
  def create
    @student = Student.new(student_params)

    respond_to do |format|
      if @student.save
        format.html { redirect_to @student, notice: 'Student was successfully created.' }
        format.json { render :show, status: :created, location: @student }
      else
        format.html { render :new }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /students/1
  # DELETE /students/1.json
  def destroy
    @student.destroy
    respond_to do |format|
      format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def student_params
      params.require(:student).permit(:name)
    end
end

如果你需要别的东西只是评论,我会提供 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

简短回答是:

...
<% @submissions.each do |submission| %>
    <tr>
      <td><%= submission.conflict %></td>
      <td><%= submission.computer %></td>
      <td><%= submission.extra_time %>%</td>
      <td><%= submission.am_pm %></td>
      <td><%= submission.students.first.name %></td>
    </tr>
<% end %>
...

如果提交没有学生,则不会收到错误

<td><%= submission.students.first.try(:name) %></td>

很长的答案是更改分配,只添加一列提交链接学生(student_id)并删除加入表(submissionstudent),因为每次提交时总有一名学生。

编辑:

显示所有学生的姓名,你可以这样做

<td><%= submission.students.pluck(:name).join(' - ') %></td>

或者如果你需要更多,你可以迭代学生

<% @submissions.each do |submission| %>
    <tr>
      <td><%= submission.conflict %></td>
      <td><%= submission.computer %></td>
      <td><%= submission.extra_time %>%</td>
      <td><%= submission.am_pm %></td>
      <td>
        <% submission.students.each do |ss| %>
          <%= ss.name %> - <%= ss.last_name %>
        <% end %>
      </td>
    </tr>
<% end %>