classequalsarthur classequalsarthur - 2 years ago 73
Ruby Question

Ruby On Rails: +1 option/vote button error

I'm learning ROR and working on a voting style application, I've got my four tables up and running Users, Questions, Options and Answers and I can write to the Answers table via MySQL and the total votes are displaying so far which is great.

I'm having a problem with getting a +1 button working (eventually it'll be unique per user but for now I just want to see it working).

I'm fairly certain my files are all setup correctly but I'm confused with what to put in my Questions "Show" view for the button, it might be a routes problem as it does say

undefined method 'upvote_option_path'
- any help would be greatly appreciated! If I've forgotten any files below let me know :-)

Question's Show View:

<% @question.options.each_with_index do |option, index| %>
<p><%= option.option_text %></p>
<p><%= pluralize(option.answers.count, "vote") %><br><br></p>
<p><%= button_to '+1', upvote_option_path(option), method: :post %></p>
<% end %>

Option's Controller upvote:

def upvote
@question = Question.find(params[:question_id])
@option = Option.find(params[:option_id])
Answer.create(user_id:, question_id: @question, option_id: @option)

Routes file:

resources :questions do
resources :options do
post 'upvote'

rake routes:

question_option_upvote POST /questions/:question_id/options/:option_id/upvote(.:format) options#upvote

Answer Source

undefined method upvote_option_path

According to your routes, there is no upvote_option_path, it should be question_option_upvote_path. Also the path takes two arguments as keys(:question_id & :option_id), so you need to pass those two to the path. The below should work

<%= button_to '+1', question_option_upvote_path(@question, option), method: :post %></p>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download