Rails 3 Jquery Star Rating plugin problem submitting
I'm trying to use Rails 3 and JQuery Star Rate plugin http://orkans-tmp.22web.net/star_rating/index.html#main-menu=1&demo-tabs=1 . The problem is that the hidden field generated by the plugin is not being submitted in the rails form
<%= form_for(@wine) do |f| %>
<h3 class="h3wine"></h3>
<div class="field">
<%= f.label :name , :class => "left"%>
<label class="right"><%= f.text_field :name %> </label><br />
</div>
<div class="note_fields">
<h2 style="margin-left: 20%;">Note</h2>
<% f.fields_for :notes do |builder| %>
<%= render 'note_fields', :f => builder %>
<% end %>
</div>
<span>
<%= f.submit %>
</span>
<% end %>
Then the partial note_fields.html.erb is like
<script>
$("#stars-wrapper1").stars();
</script>
<p>
<div class="field">
Rating: <span id="stars-cap"></span>
<div id="stars-wrapper1">
<br>
<% [ '1', '2', '3', '4', '5' ].each do |rates| %>
<%= f.radio_button :rating, rates%>
<% end %>
</div>
</div>
<div class="field">
<%= f.label :text, :class => "left" %>
<label class="right"><%= f.text_area :text,:size => "60x12" %></label><br />
</div>
</p>
So everything is ok, related to displaying the stars and show the value of "rating" from database. The problem is when submitting in the log file the field rating doesn't come.
The generated hiiden field is like this:
<input type="hidden" value="5" name="wine[notes_attributes][0][rating]" disabled="">
Which get well updated once the stars are changed.
In the log this what shows:
Parameters: {"commit"=>"Update Wine", "wine"=>{"name"=>"Borba", "notes_attributes"=>{"0"=>{"text"=>"borbocas", "id"=>"6666"}}}, "authenticity_token"=>"/FsKTnSm2eAvPQiXie
Oh. If it 开发者_StackOverflow社区helps , If I don't use the plugin and mantain the radio_buttons it work ok.
Thanks for the help :)
Update - Thanks to all of you -
Exactly @Kris :) I realizes that yesterday night .. but I was too tired to post the answer . Thanks a lot :)
So I've bind a javascript function to the submit event form. So when the form is submitted the attribute disabled is remove. Because every time you click on the stars this hidden field is updated as so the disabled field is inserted
<script>
$("form").submit(function () {
$("input[name^=\"wine[notes_attributes][0][rating]\"]").removeAttr("disabled");});
$("#stars-wrapper1").stars();
</script>
Thanks guys :)
Check your jquery version, star rating supports jquery up to 1.5.2. Newer jquery versions does fall into this bug.
The problem is that the generated hidden field has a disabled attribute and disabled fields are not valid for submission.
Read this: http://www.w3.org/TR/html4/interact/forms.html#h-17.12
I'm having this issue myself using jQuery UI Stars v3.0.1, jQuery 1.6.1 and still trying to figure out a solution to this.
UPDATE:
OK, I've came up with a solution. It's a hack, but it fixes the issue.
Basically I'm using jQuery to go through the hidden inputs and remove the disabled attribute:
$("input[type=hidden]").removeAttr("disabled");
looks like that attribute should be getting posted.
On your model do you need something like this?
attr_accessible :rating
Or are you limiting what is accessible in any way?
I'm running JQuery 1.7.2 and JQuery UI Stars 3.0.1.
It seems the regression is related to how JQuery handles boolean attributes now...
Here are my changes:
73: o.disabled = o.disabled || (o.isSelect ? $(self.$selec).is(':disabled') : $(this).is(':disabled'));
and
155: self.$value.attr({disabled: o.disabled, value: o.value});
精彩评论