1

The following line is generated by jquery in the DOM when a user clicks to make a 5 star rating:

<input type="hidden" name="review[answer01]" id="review_answer01" value="5">

Somehow my controller is not picking up this value for params[:review][:answer01]

It seems to me that when I click the submit button the hidden value isn't being sent at all. Is the form not aware of the hidden field because it's not present at the start? How do I fix this.

Everything worked fine with a simple radio button select.

BTW I manually added the same line of code to my form and it worked. I put

<%= f.hidden_field :answer01, :value => 5 %>

which produced html:

<input id="review_answer01" name="review[answer01]" type="hidden" value="5">

I just don't get it...

EDIT:

Here is the js source Here is the js: https://github.com/wbotelhos/raty/blob/master/js/jquery.raty.js

The input tags are generated by line 62, which I modded slightly to give an ID ( I wasn't sure if that was the issue ).

My line 62: self.score = $('<input />', { type: 'hidden', name: self.opt.scoreName, id: self.opt.scoreId}).appendTo(self);

I also added scoreID : 'blank' to $.fn.raty.defaults {} to provide an option to set ID.

Now, inside my view I have:

<table class='table'>
  <tbody>

  <%= simple_form_for @review, :html => { :multipart => true } do |f| %>

    <td style='width:115px; padding-top:10px;'><div id="s1"></td>
    <td><h3 style = 'padding-bottom:3px;'><%= @questions[0] %></h3></td>

    [...] (repeated for other questions)

  </tbody>
</table>

  <div style='margin-left:80px; margin-top:20px; margin-bottom:200px;' id='subbut'>
    <%= f.button :submit, "Submit Review", :class=>'btn btn-large' %>
  </div>
  <% end %>

<script>
$('#s1').raty({
    scoreName : 'review[answer01]',
    scoreId : 'review_answer01'
});
</script>

What I see in firebug DOM analysis with 5/5 star selected:

<td style="width:115px; padding-top:10px;">
<div id="s1" style="cursor: pointer; width: 100px; ">
  <img src="/assets/star-on.png" alt="1" title="bad">&nbsp;
  <img src="/assets/star-on.png" alt="2" title="poor">&nbsp;
  <img src="/assets/star-on.png" alt="3" title="regular">&nbsp;
  <img src="/assets/star-on.png" alt="4" title="good">&nbsp;
  <img src="/assets/star-on.png" alt="5" title="gorgeous">
  <input type="hidden" name="review[answer01]" id="review_answer01" value="5">
</div>
</td>

FORM TAG:

<form accept-charset="UTF-8" action="/reviews" class="simple_form new_review" enctype="multipart/form-data" id="new_review" method="post" novalidate="novalidate"></form>
1
  • That seems fine, could we have the JS that makes it and/or the surrounding form HTML? May 13, 2012 at 0:52

1 Answer 1

1

Your HTML structure isn't right... Look:

<table class='table'>
  <tbody>

  <%= simple_form_for @review, :html => { :multipart => true } do |f| %>

    <td style='width:115px; padding-top:10px;'><div id="s1"></td>
    <td><h3 style = 'padding-bottom:3px;'><%= @questions[0] %></h3></td>

    [...] (repeated for other questions)

  </tbody>
</table>

  <div style='margin-left:80px; margin-top:20px; margin-bottom:200px;' id='subbut'>
    <%= f.button :submit, "Submit Review", :class=>'btn btn-large' %>
  </div>
  <% end %>

The form starts inside the table, but ends outside. this will cause the HTML to be parsed as not including all the inputs and some general weirdness because the browser can't figure out what you meant.

Incidentally, using HAML would circumvent that kind of problems, but to each his own :)

3
  • Nice, I had another row outside of the form which semantically didn't belong there.. that's why the table started earlier. However, I now have it within the form, and everything works fine. Thanks Felix!
    – Abram
    May 13, 2012 at 2:12
  • Well, to be a pedant, the table doesn't semantically belong anywhere near here :) May 13, 2012 at 2:26
  • 1
    Aww, but the twitter bootstrap tables make forms look so GOOD!
    – Abram
    May 13, 2012 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.