0

我对 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 不熟悉,而且似乎有些东西从裂缝中溜走了。

4

2 回答 2

3

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

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

对此

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

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

另外,请确保

resources :notes

在您的 routes.rb 文件中

于 2013-07-30T21:28:11.357 回答
0

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

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

于 2013-07-30T21:15:23.033 回答