无法使多态模型关联起作用

时间:2016-03-15 16:11:44

标签: ruby-on-rails ruby ruby-on-rails-4

我正在创建一个多态模型,但它仍然没有保存,并进入

  

未定义的方法notes_path错误

我曾经能够摆脱这个错误,但仍然没有保存,并且调用回滚事务。监听器应该能够在消息和消息部分下创建笔记。有人可以帮帮我吗?

class NotesController < ApplicationController
  before_action :find_notes, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_listener!

  def new
    @note = Note.new
  end     

  def index
    @notes = Note.all
  end

  def show
    @note = Note.find(params[:id])
  end

  def create    
    # @notable = find_notable
    # @note = @notable.notes.build(notes_params)
    @note = @notable.notes.new notes_params
    @note.listener = current_listener
    @note.save
    redirect_to @notable, notice: "your note sas saved"

    # if @note = Note.create(notes_params)
    #   redirect_to notes_path
    # end    
  end

  private
    def notes_params
      params.require(:note).permit(:content)
    end

    def find_notes
      @note = Note.find(params[:id])
    end        
end

我的笔记模型

class Note < ActiveRecord::Base
  belongs_to :notable, polymorphic: true
  belongs_to :listener
  validates :listener, :presence => true
  validates :notable, :presence => true
end

我的表格

<%= simple_form_for [notable, Note.new] do |f| %>
  <%= f.text_area :content, label: "Message Title"  %>
  <%= f.button :submit, style: "margin-bottom: 80px" %>
<% end %>

我的控制器/消息/ notes_controller.rb

class Messages::NotesController < NotesController
  before_action :set_notable

  private    
    def set_notable
      @notable = Message.find(params[:message_id])
    end
end

我的留言模型:

class Message < ActiveRecord::Base
  belongs_to :speaker
  has_many :message_categories
  has_many :categories, through: :message_categories
  has_many :messageparts, dependent: :destroy
  has_many :notes, as: :notable
end

我的索引页面:

<% @notes.each do |note| %>
  <%= note.content %>
<% end %>

已更新! 我的Routes.rb

resources :messages do
    resources :messageparts
      resources :notes
  end

0 个答案:

没有答案