Question

When trying to access the Reasons Index page

NameError in Reasons#index

Showing /home/alec/workspace/rails/nwis/app/views/reasons/index.html.erb where line #26 raised:

undefined local variable or method `upvote_reason' for #<#<Class:0x000000023d9940>:0x000000023d3298>
Extracted source (around line #26):

23:     <td><%= link_to 'Show', reason %></td>
24:     <td><%= link_to 'Edit', edit_reason_path(reason) %></td>
25:     <td><%= link_to 'Destroy', reason, :confirm => 'Are you sure?', :method => :delete %></td>
26: <td><%= link_to "Vote up", :action => upvote_reason, :id => reason.id %></td>
27:   </tr>
28: <% end %>
29: </table>
Rails.root: /home/alec/workspace/rails/nwis

Application Trace | Framework Trace | Full Trace
app/views/reasons/index.html.erb:26:in `block in _app_views_reasons_index_html_erb__2084449067598068298_18776480__4598497200926455905'
app/views/reasons/index.html.erb:16:in `each'
app/views/reasons/index.html.erb:16:in `_app_views_reasons_index_html_erb__2084449067598068298_18776480__4598497200926455905'
app/controllers/reasons_controller.rb:7:in `index'

The line in the view:

<td><%= link_to "Vote up", :action => upvote_reason, :id => reason.id %></td>

The method in the controller:

def upvote_reason
    @reason = Reason.find(params[:id])
    @reason.upvote += 1
    @reason.save
    redirect_to reason_path
end

Admittedly, I'm pretty new at Rails, but I feel like I'm on the verge of it clicking, once I can figure out how to properly interact with the controller. If someone can point me in the right direction to a tutorial that covers this topic well, I'd be glad to check it out if this is too basic of a question.

Was it helpful?

Solution

Put a colon before it to turn it into a symbol, or put quotes around it:

:action => :upvote_reason

or

:action => "upvote_reason"

Also, you'll need to update your routes. Whereas you currently probably just have resources :reasons you'll want something like this:

resources :reasons do
  get "upvote_reason", :on => :member
end

In fact, after adding this route, you can just use the named route instead of your :action and :id params:

<%= link_to "Vote up", upvote_reason_reason_path(reason) %>

(If you just rename your upvote_reason action to upvote, then this would become upvote_reason_path(reason))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top