I want to store multiple questions and answers with a unique player ID in one database column.
Database column player_answer should look like-
player_id | question | answer
1 | India | New Delhi
1 | USA | Washington D.C.
1 | UK | London
..so on and so forth.
This is my /views/player_answers/_form.html.erb
<%= form_for(@player_answer) do |f| %>
<% if @player_answer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@player_answer.errors.count, "error") %> prohibited this player_answer from being saved:</h2>
<ul>
<% @player_answer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :player_id %><br />
<%= f.text_field :player_id, :readonly => true, :value => Player.find(params[:playerid]).id %>
<!--<#%= f.text_field :player_id %>-->
</div>
<% (1..10).each do |i| puts i %>
<table>
<tr>
<td>
<div class="field">
<%= f.label :question_id %><br />
<%= f.text_field :question_id %>
</div>
</td>
<td>
<div class="field">
<%= f.label :answer %><br />
<%= f.text_field :answer, :readonly => true, :value => params[:playeranswerid].to_i %>
</div>
</td>
</tr>
</table>
<% end %>
<div class="field">
<%= f.label :answer_after_self_scoring %><br />
<%= f.text_field :answer_after_self_scoring %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And here is my player_answers_controller.rb file code- class PlayerAnswersController < ApplicationController # GET /player_answers # GET /player_answers.xml def index @player_answers = PlayerAnswer.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @player_answers }
end
end
# GET /player_answers/1 # GET /player_answers/1.xml def show @player_answer = PlayerAnswer.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @player_answer }
end
end
# GET /player_answers/new # GET /player_answers/new.xml def new @player_answer = PlayerAnswer.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @player_answer }
end
end
# GET /player_answers/1/edit def edit @player_answer = PlayerAnswer.find(params[:id]) end
# POST /player_answers # POST /player_answers.xml def create @player_answer = PlayerAnswer.new(params[:player_answer])
respond_to do |format|
if @player_answer.save
format.html { redirect_to(@player_answer, :notice => 'Player answer was successfully created.') }
format.xml { render :xml => @player_answer, :status => :created, :location => @player_answer }
else
format.html { render :action => "new" }
format.xml { render :xml => @player_answer.errors, :status => :unprocessable_entity }
end
end
end
# PUT /player_answers/1 # PUT /player_answers/1.xml def update @player_answer = PlayerAnswer.find(params[:id])
respond_to do |format|
if @player_answer.update_attributes(params[:player_answer])
format.html { redirect_to(@player_answer, :notice => 'Player answer was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @player_answer.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /player_answers/1 # DELETE /player_answers/1.xml def destroy @player_answer = PlayerAnswer.find(params[:id]) @player_answer.destroy
respond_to do |format|
format.html { redirect_to(player_answers_url) }
format.xml { head :ok }
end
end end
Please let me know how to go ahead and solve this problem.
P.s. I have just started using rails and this is probably the first project I am working live on. Please help!!