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

时间:2022-10-08 23:53:51

I'm pretty new to Ruby on Rails so forgive me if this mistake is pretty amateur. I'm trying to create a section of a little website where people can take notes and have it record the author, the text, and the subject. When I try to access it on localhost, I get the following error:

我是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>

Here's the code for the related files

这是相关文件的代码

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

If anyone could help out, I'd appreciate it a lot. I feel like it's a very simple, beginner issue but I'm just unfamiliar with RoR and something seems to have slipped through the cracks.

如果有人能提供帮助,我会非常感激。我觉得这是一个非常简单的初学者问题,但我对RoR并不熟悉,似乎已经陷入了困境。

2 个解决方案

#1


3  

I don't think you actually included the section of code that is causing the problem, but based on the error you posted changing:

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

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

to this

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

should help solve your problem.

应该有助于解决您的问题。

Also, make sure that

另外,请确保

resources :notes

is in your routes.rb file

在您的routes.rb文件中

#2


0  

is line 12 in notes_controller.rb there or just a note? if so, delete it, or at least comment it out.

在notes_controller.rb中是第12行还是只是一个音符?如果是这样,删除它,或至少将其注释掉。

also, does your error point to a file and line number? if so can you add it and the relevant line(s)

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

#1


3  

I don't think you actually included the section of code that is causing the problem, but based on the error you posted changing:

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

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

to this

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

should help solve your problem.

应该有助于解决您的问题。

Also, make sure that

另外,请确保

resources :notes

is in your routes.rb file

在您的routes.rb文件中

#2


0  

is line 12 in notes_controller.rb there or just a note? if so, delete it, or at least comment it out.

在notes_controller.rb中是第12行还是只是一个音符?如果是这样,删除它,或至少将其注释掉。

also, does your error point to a file and line number? if so can you add it and the relevant line(s)

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