简体   繁体   English

使用嵌套资源#rails创建一个新对象

[英]creating a new object with nested resources #rails

I have a rails application that creates a notebook and within the notebook, notes. 我有一个Rails应用程序,它创建一个笔记本,并在笔记本中创建便笺。 I am writing a form to create a new note but I am confused how to the format the form. 我正在写一个表格来创建新的笔记,但是我很困惑如何格式化表格。 This is what I currently have: 这是我目前拥有的:

notebook/id/note/new -> view 笔记本/ ID /笔记/新->视图

= form_for [@notebook, @note] do |note|
  == note.label :title
  == note.text_field :title
  br
  == note.text_field :body
  br
  == note.submit :save

NotesController -> NotesController->

  def new
    @note = Note.new
  end


 def create
    @note = @notebook.notes.create(notes_params)
    @note.user = current_user
    redirect_to @notebook
 end

  my routes: 
     resources :notebooks  do
       resources :notes 
     end

The error I am receiving says 我收到的错误说

 undefined method `notes_path' for #objectnumber

Since notes is a nested resource under notebooks , the form_for method needs a little help with the url. 由于notesnotebooks下的嵌套资源,因此form_for方法需要有关url的一些帮助。 It's trying to create a path from the note passed into the block, but you need to specify that the url is nested, like so: 它试图从传递到块中的note创建路径,但是您需要指定url是嵌套的,如下所示:

= form_for [@notebook, @note], url: notebook_note_path(@notebook, @note) do |note|

See the Rails Guides page on creating urls and paths for more detailed info. 有关更多详细信息,请参见关于创建URL和路径Rails指南页面

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

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