我想在一个数据库列中使用唯一的玩家ID存储多个问题和答案。
数据库列player_answer应该看起来像 -
player_id |问题|答案
1 |印度|新德里
1 |美国|华盛顿特区。
1 |英国|伦敦
......等等。
这是我的/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 %>
这是我的player_answers_controller.rb文件代码 - class PlayerAnswersController&lt; ApplicationController的 #GET / player_answers #GET /player_answers.xml def指数 @player_answers = PlayerAnswer.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @player_answers }
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
端
#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
端
#GET / player_answers / 1 /编辑 def编辑 @player_answer = PlayerAnswer.find(params [:id]) 端
#POST / player_answers #POST /player_answers.xml def创建 @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
端
#PUT / player_answers / 1 #PUT /player_answers/1.xml def更新 @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
端
#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
端 端
请让我知道如何继续解决这个问题。
P.S。我刚刚开始使用rails,这可能是我正在进行的第一个项目。请帮忙!!
答案 0 :(得分:1)
如果要在一列中添加多个数据,请使用'serialize'。
根据您的喜好,这会将所有内容存储为哈希或数组。
请参阅此处的文档:http://api.rubyonrails.org/classes/ActiveRecord/Base.html
答案 1 :(得分:0)
从你的问题中不清楚你有什么麻烦
当您尝试使用相同的player_id创建新记录时是否会出错?如果是这样 - 确保player_id没有uniq索引或不是主键
为你的布局创建表/模型试试这个:
script/generate migration create_player_answers player_id:integer question:string answer:string
或rais generate
- 因为您没有指定您正在使用的导轨版本