开发者

Trouble getting serialize to work in a form

I have a form where values for a serialized field are chosen by a series of drop-down boxes. I've been struggling with this for quite a few hours, trying to get it to work nicely. There are a couple problems.

  1. TypeError (expected Hash (got String) for param 'rankings' upon submit.
  2. What is the best way to send this form to create if the record is new, or update if it is existing? (Current implementation shown).

The company specifies multiple criteria, such as "overall" or "cost" or "performance". Each of these criteria has a drop-down in this form. The user chooses his/her rankings for each of the criteria, and the results get saved in @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 %>

Html source

<form accept-charset="UTF-8" action="/reviews/21?ci=21&amp;jd=37&amp;rank=1&amp;count=1&amp;key=104fdf0f23712e69e0e7acd507ea4eeb&amp;mkey=e6ec509f1f13ed00f9e0b07b2daeced6" class="review_edit" id="review_edit" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><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>     
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜