NameError - 在控制器中调用创建的方法时未定义的局部变量或方法

时间:2021-08-31 23:52:58

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.

不可否认,我在Rails上很新,但是我觉得我已经处于点击状态,一旦我能弄清楚如何正确地与控制器交互。如果有人可以指出我正确的方向来完成一个涵盖这个主题的教程,我很高兴看看这是否是一个问题的基础。

1 个解决方案

#1


3  

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:

实际上,添加此路由后,您只需使用命名路由而不是:action和: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))

(如果你只是将你的upvote_reason动作重命名为upvote,那么这将成为upvote_reason_path(reason))

#1


3  

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:

实际上,添加此路由后,您只需使用命名路由而不是:action和: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))

(如果你只是将你的upvote_reason动作重命名为upvote,那么这将成为upvote_reason_path(reason))