Skip to content

Weird bug with our responses #34

@DanaMC18

Description

@DanaMC18

Hey Anna,

So on our page where students respond to an exit ticket's question, we are having a weird bug. We are using .each inside our form to list all the questions for a particular exit ticket along with a field for responses.

We set up an if/else inside the .each inside the form so that if a question has been responded to, the input field disappears and the student is notified that they have responded (we are redirecting to the same page similar to how we have on our controller). However, this is answering the questions in reverse order. If someone responds in the first input field for the first question, the LAST question is what then disappears and indicates a response. It is so weird and narly.

To try and solve this I created a method on the questions controller to try and order them, but its not helping, its doing the same thing.

This is the responses controller:

def new 
        @exit_ticket = ExitTicket.find_by(id: params[:exit_ticket_id])
        @questions = Question.where(exit_ticket_id: params[:exit_ticket_id])
        @response = Response.new
    end

    def create
        @response = Response.create({
            response: params[:response], 
            exit_ticket_id: params[:exit_ticket_id], 
            question_id: params[:question_id]
            })
        @exit_ticket = ExitTicket.find(params[:exit_ticket_id])
        redirect_to new_exit_ticket_response_path(@exit_ticket)
    end

This is the new view page for responses:

<%=form_tag "/exit_tickets/#{@exit_ticket.id}/responses" do %>
  <% @questions.ordered.each do |question|%>

    <% if Response.find_by(question_id: question.id) %>
        <br>
        <%=label_tag%> <%=question.question.capitalize%>
        <%=label_tag "You responded" %>
    <% else %>
        <br>
        <%=label_tag%> <%=question.question.capitalize%>
        <br>
        <%= text_area_tag :response, nil, size: "50x5" %>
        <br>
        <%= hidden_field_tag "question_id", "#{question.id}" %>
        <%=submit_tag "Submit Response" %>
    <% end %> <!-- if/else -->

  <% end %> <!-- .each -->
<% end %> <!-- form -->

And this is the method I made for QUESTIONS to order them:

def self.ordered
    all.sort {|x,y| x.created_at <=> y.created_at}
  end

Note: Instead of sort I was trying to originally use .order, but it wasn't working at all.

If you could shed some light on how to fix this weird bug, we'd really appreciate it. In the meantime I'm going to brainstorm some other ways to do it.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions