I am trying to create a personality test.
I have a Questions table that looks like this
ID | Question |
----------------------------
1 | How likely would you etc...
and a Results table that looks like this
ID | Answer | Question_ID | User_Id
------------------------------------------------
1 | 1 | 1 | 1
I have 68 questions that I want to loop through and I want to store the answers (which are integers on a scale from 1-10) in my Results table.
How do I create a form that will save data to my Results table?
I am having trouble populating the answer column of my Results table.
Here's what I have. It doesn't work.
<%= form_with(model: @result, local: true) do |form| %>
<% @questions.each do |question| %>
<div>
<h4><%= question.id %>. <%=question.question %></h4><br />
<div class="hidden-field">
<%= form.hidden_field :question_id, value: question.id %>
</div>
<div class="field">
<%= form.number_field :answer, placeholder:'Please answer on a scale from 1 to 10' %>
</div>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
In my controller, I have a Home Controller (to display a home page)
class HomeController < ApplicationController
def index
@questions = Question.all.order(:created_at)
@user = User.new
@result = Result.new
end
end
I also have my Results Controller
class ResultsController < ApplicationController
before_action :set_result, only: [:show, :edit, :update, :destroy]
def new
@result = Result.new
end
def create
@result = Result.new(result_params)
end
private
def set_result
@result = Result.find(params[:id])
end
def result_params
params.require(:result).permit(:answer, :user_id, :question_id)
end
end
The questions are displaying perfectly fine. I can even store the data, but only one record gets saved and it only populates the question_id column with 68 and leaves the answer column NULL
What am I doing wrong?