How to create a form that is for a poll that shows if you have voted or not. Given a poll like so:
What is the best camera manufacturer?
- HP
- Kodak
- Cannon
I have the following models:
Poll (question)
PollOption (poll_id, title)
PollVote(user_id, poll_id, poll_option_id)
My question is how do I output a form with the question/options a开发者_StackOverflow中文版long with radio buttons to allow voting?
Here's what I have below. I'm stuck on how to build the radio buttons? Thanks
<div class="question">
<%= @poll.title %>
</div>
<div class="choices">
<% @poll.poll_options.each do |poll_option| %>
<div class="row clearfix">
<div class="pollRadioBtn">
<input type="radio" name="option_id" value="" id="">
</div>
<div class="pollVotesBar">
<%= poll_option.title %>
</div>
</div>
<% end %>
</div>
Routes:
resources :polls do
resources :poll_options do
resources :poll_votes
end
end
jQuery to post a vote:
$.ajax({
type: 'POST',
url: '/polls/<%=@poll.id%>/poll_options/' + $(this).val() + '/poll_votes',
data: 'poll_vote[poll_id]=' + <%=@poll.id%> + '&poll_vote[poll_option_id]=' + $(this).val()
});
i would advise you check out this project where they created a poll system it is help full because it helped me to acomplish my own risealumni
精彩评论