繁体   English   中英

Ruby on Rails-未定义的局部变量或方法

[英]Ruby on Rails - undefined local variable or method

我是Ruby on Rails的新手,所以如果这个错误很业余,请原谅我。 我正在尝试创建一个小型网站的一个部分,人们可以在其中做笔记,并记录作者,正文和主题。 当我尝试在localhost上访问它时,出现以下错误:

line #25 raised:
undefined local variable or method `note_path' for #<#<Class:0x0000000be44598>:0x00000008648ce8>
25:                         <li><%= link_to 'Create Notes', note_path %></li>

这是相关文件的代码

new.html.erb:
  1 <%= form_for :note, url: posts_path do |f| %>
  2   <p>
  3     <%= f.label :subject %><br>
  4     <%= f.text_field :subject %>
  5   </p>
  6 
  7   <p>
  8     <%= f.label :note %><br>
  9     <%= f.text_area :note %>
 10   </p>
 11 
 12   <p>
 13     <%= f.submit %>
 14   </p>
 15 <% end %>

show.html.erb:
  1 <p>
  2   <strong>Subject:</strong>
  3   <%= @note.subject %>
  4 </p>
  5 
  6 <p>
  7   <strong>Note:</strong>
  8   <%= @post.note %>
  9 </p>

notes_controller.rb:
1 class NotesController < ApplicationController
2   def new
3   end 
4   def show
5     @note = Note.find(params[:id])
6   end
7   def create
8     @note = Note.new(params[:note].permit(:subject, :note))
9     
10     @note.save
11     redirect_to @note
12   end
13   note GET  /notes/:id(.:format)  notes#show
14   private
15     def note_params
16        params.require(:note).permit(:subject, :note)
17     end
18 end 

notes.rb:
  1 class Notes < ActiveRecord::Base
  2   attr_accessible :note, :subject
  3 end

如果有人可以帮忙,我会非常感激。 我觉得这是一个非常简单的初学者问题,但是我对RoR并不熟悉,似乎有些东西已经溜走了。

我认为您实际上并未包含导致问题的代码部分,但根据您发布的错误进行了更改:

<li><%= link_to 'Create Notes', note_path %></li>

对此

<li><%= link_to 'Create Notes', new_note_path %></li>

应该可以帮助您解决问题。

另外,请确保

resources :notes

在您的routes.rb文件中

是notes_controller.rb中的第12行还是仅是注释? 如果是这样,请将其删除,或者至少将其注释掉。

另外,您的错误是否指向文件和行号? 如果可以,您可以添加它和相关的行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM