我有一个表单,其中序列化字段的值由一系列下拉框选择。我已经在这个问题上苦苦挣扎了好几个小时,试图让它很好地工作。有几个问题。
TypeError (expected Hash (got String) for param 'rankings'
。create
的最佳方式是什么,或update
如果存在? (显示当前实施)。公司指定了多个标准,例如“整体”或“成本”或“绩效”。这些标准中的每一个都有这种形式的下拉列表。用户为每个条件选择他/她的排名,结果保存在@ review.rankings(serialized)中。
# controller
@review = Review.find_or_initialize_by_user_id(current_user.id)
@rankings = @review.rankings # if @review exists, these contain this user's current choices
@criteria = @company.criteria # each has its own dropdown
# show.html.erb
<% @review.id.blank? ? (action = "create") : (action = "update") %>
<%= form_for @review, :as => :review, :url => params.merge(:controller => "reviews", :action => action, :id => @review.try(:id)) do |f| %>
<%= f.error_messages %>
<% @criteria.each do |label| %>
<%= f.fields_for :rankings do |f| %>
<%= f.text_field label, :value => @review.rankings.present? ? (@rankings[label]) : nil %>
<%= label %>:
<select id="review_rankings_<%= label %>" name="review[rankings][<%= label %>]">
<option value="0" <%= @rankings.present? && @rankings[label] == "0" ? "selected='selected'" : nil %>>Very poor</option>
<option value="1" <%= @rankings.present? && @rankings[label] == "1" ? "selected='selected'" : nil %>>Poor</option>
<option value="2" <%= @rankings.blank? || @rankings[label] == "2" ? "selected='selected'" : nil %>>Average</option>
<option value="3" <%= @rankings.present? && @rankings[label] == "3" ? "selected='selected'" : nil %>>Good</option>
<option value="4" <%= @rankings.present? && @rankings[label] == "4" ? "selected='selected'" : nil %>>Very Good</option>
<option value="5" <%= @rankings.present? && @rankings[label] == "5" ? "selected='selected'" : nil %>>Exceptional</option>
</select>
<% end %>
<% end %>
<%= submit_tag "Update", :class=>"greenButton" %>
<% end %>
<form accept-charset="UTF-8" action="/reviews/21?ci=21&jd=37&rank=1&count=1&key=104fdf0f23712e69e0e7acd507ea4eeb&mkey=e6ec509f1f13ed00f9e0b07b2daeced6" class="review_edit" id="review_edit" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="S89rpLRSqye18RAD3cjdAD83Vvn0oFubooMhA7AX7Ms=" /></div>
performance:
<select id="review_rankings_performance" name="review[rankings][performance]">
<option value="0" >Very poor</option>
<option value="1" >Poor</option>
<option value="2" >Average</option>
<option value="3" >Good</option>
<option value="4" >Very Good</option>
<option value="5" >Exceptional</option>
</select>
<input class="greenButton" name="commit" type="submit" value="Update" />
</form>