Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Can';我不知道我的路由错误是从哪里来的_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Can';我不知道我的路由错误是从哪里来的

Ruby on rails Can';我不知道我的路由错误是从哪里来的,ruby-on-rails,ruby,Ruby On Rails,Ruby,所以我第一次尝试实现多态关联,但遇到了一点麻烦 我试图允许用户在联系人或组织上留下便条。但是在我提交了一张便条之后,我遇到了一个路由错误 以下是我的路线: 这是我的routes.rb文件: Rails.application.routes.draw do get 'welcome/index' resources :organizations do resources :contacts do resources :notes, module: :contac

所以我第一次尝试实现多态关联,但遇到了一点麻烦

我试图允许用户在联系人或组织上留下便条。但是在我提交了一张便条之后,我遇到了一个路由错误

以下是我的路线

这是我的routes.rb文件:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :organizations do
    resources :contacts do 
      resources :notes, module: :contacts
    end
    resources :notes, module: :organizations
  end

  root 'welcome#index'
end
class NotesController < ApplicationController

  def create
    @note = @noteable.notes.new note_params

    redirect_to @noteable, notice: "Your note was successful!!!"
  end

  private

  def note_params
    params.require(:note).permit(:note_title, :note_body)
  end
end
class Contacts::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Contact.find(params[:contact_id])
  end
end
class Organizations::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Organization.find(params[:organization_id])
  end
end
<h1>New Note</h1>
<%= form_for [noteable, Note.new] do |form| %>

  <p>
    <%= form.label :note_title %><br>
    <%= form.text_field :note_title %>
  </p>
  <p>
    <%= form.label :note_body %><br>
    <%= form.text_area :note_body %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>
这是我的notes\u controller.rb文件:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :organizations do
    resources :contacts do 
      resources :notes, module: :contacts
    end
    resources :notes, module: :organizations
  end

  root 'welcome#index'
end
class NotesController < ApplicationController

  def create
    @note = @noteable.notes.new note_params

    redirect_to @noteable, notice: "Your note was successful!!!"
  end

  private

  def note_params
    params.require(:note).permit(:note_title, :note_body)
  end
end
class Contacts::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Contact.find(params[:contact_id])
  end
end
class Organizations::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Organization.find(params[:organization_id])
  end
end
<h1>New Note</h1>
<%= form_for [noteable, Note.new] do |form| %>

  <p>
    <%= form.label :note_title %><br>
    <%= form.text_field :note_title %>
  </p>
  <p>
    <%= form.label :note_body %><br>
    <%= form.text_area :note_body %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>
class NotesController
这是我的联系人/notes\u controller.rb文件:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :organizations do
    resources :contacts do 
      resources :notes, module: :contacts
    end
    resources :notes, module: :organizations
  end

  root 'welcome#index'
end
class NotesController < ApplicationController

  def create
    @note = @noteable.notes.new note_params

    redirect_to @noteable, notice: "Your note was successful!!!"
  end

  private

  def note_params
    params.require(:note).permit(:note_title, :note_body)
  end
end
class Contacts::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Contact.find(params[:contact_id])
  end
end
class Organizations::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Organization.find(params[:organization_id])
  end
end
<h1>New Note</h1>
<%= form_for [noteable, Note.new] do |form| %>

  <p>
    <%= form.label :note_title %><br>
    <%= form.text_field :note_title %>
  </p>
  <p>
    <%= form.label :note_body %><br>
    <%= form.text_area :note_body %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>
class Contacts::NotesController
这是我的组织/notes\u controller.rb文件:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :organizations do
    resources :contacts do 
      resources :notes, module: :contacts
    end
    resources :notes, module: :organizations
  end

  root 'welcome#index'
end
class NotesController < ApplicationController

  def create
    @note = @noteable.notes.new note_params

    redirect_to @noteable, notice: "Your note was successful!!!"
  end

  private

  def note_params
    params.require(:note).permit(:note_title, :note_body)
  end
end
class Contacts::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Contact.find(params[:contact_id])
  end
end
class Organizations::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Organization.find(params[:organization_id])
  end
end
<h1>New Note</h1>
<%= form_for [noteable, Note.new] do |form| %>

  <p>
    <%= form.label :note_title %><br>
    <%= form.text_field :note_title %>
  </p>
  <p>
    <%= form.label :note_body %><br>
    <%= form.text_area :note_body %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>
类组织::NotesController
这是我的视图/notes/_form.html.rb文件:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :organizations do
    resources :contacts do 
      resources :notes, module: :contacts
    end
    resources :notes, module: :organizations
  end

  root 'welcome#index'
end
class NotesController < ApplicationController

  def create
    @note = @noteable.notes.new note_params

    redirect_to @noteable, notice: "Your note was successful!!!"
  end

  private

  def note_params
    params.require(:note).permit(:note_title, :note_body)
  end
end
class Contacts::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Contact.find(params[:contact_id])
  end
end
class Organizations::NotesController < ApplicationController

  private

  def set_noteable
    @noteable = Organization.find(params[:organization_id])
  end
end
<h1>New Note</h1>
<%= form_for [noteable, Note.new] do |form| %>

  <p>
    <%= form.label :note_title %><br>
    <%= form.text_field :note_title %>
  </p>
  <p>
    <%= form.label :note_body %><br>
    <%= form.text_area :note_body %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>
新注释



如果你还需要什么,我会提供的!提前谢谢你

尝试更改此选项:

resources :organizations do
  resources :contacts do 
    resources :notes, module: :contacts
  end
  resources :notes, module: :organizations
end
对这些行:

resources :organizations do
  resources :contacts do 
    resources :notes
  end
  resources :notes
end
您不需要模块,因为它们已经被父资源限定了范围

同时将
联系人/notes\u controller.rb
移动到
组织/contacts/notes\u controller.rb
。确保也将视图移动到相应的文件夹中


希望有帮助:)

我这样做了,但还是有错误。现在它是NotesController#create error中的一个NoMethodError,错误消息是“nil:NilClass的未定义方法'notes'”。您需要将set_notable方法从其他控制器移动到此控制器中,并使用before#u操作筛选器。