0

I was trying to show the student name on the submission table but I don't know how to do it. If you can please help me!

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

Joint Table: Submissionstudent:

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

Show Table:

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

Submission Controller:

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

Form Controller:

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

Student Controller:

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

If you need something else just comment and I'll provide it Thanks for your help.

Giovanni Pecchio
  • 13
  • 1
  • 2
  • 8

1 Answers1

0

Short answer is:

...
<% @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 %>
...

Or to not get error if submission do not have a student

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

Long answer is to change the assosiation and only add a column to submissions to link a student (student_id) and delete the jointable (submissionstudent) because you always have one student per submission.

EDIT:

to show all student's name you can do some like this

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

or if you need more you can iterate over students

<% @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 %>
inye
  • 1,786
  • 1
  • 23
  • 31
  • If I use this it will only show the name when I chose the first name of my list but not the others – Giovanni Pecchio Dec 06 '16 at 14:42
  • And I don't get your long answer, sorry. Thanks anyways – Giovanni Pecchio Dec 06 '16 at 14:44
  • you tell me tath a submission have one students, but according to comments you have many student. The long asnwer do not work in your situation. – inye Dec 06 '16 at 14:50
  • okay, I'll try to explain it better. I have many students and I have a dropdown in my submission form to select one the students. I want the student selected to show in my show table. Hope that this is understandable. Thanks – Giovanni Pecchio Dec 06 '16 at 14:53
  • both of the edit code don't work, the first one shows only the first name and the second one it throws an error: undefined method `student' – Giovanni Pecchio Dec 06 '16 at 15:06
  • first method you say `I was trying to show the student name...`. Sencond method I edited – inye Dec 06 '16 at 15:08
  • I was trying to create new submissions and I discovered that the student_id is not connecting to the submission. The thing is that it connected once and then it's not connecting anymore. Any thoughts? – Giovanni Pecchio Dec 06 '16 at 15:12
  • this is other question, create a new question – inye Dec 06 '16 at 15:14
  • Question: [link](http://stackoverflow.com/questions/40998831/connect-student-id-to-submission-id-in-rails) – Giovanni Pecchio Dec 06 '16 at 15:22