rails中的Nomethod错误

时间:2016-03-07 07:36:46

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

我试图让登录的听众发布他们的冥想笔记。因此,当他们登录时,他们只会看到他们的笔记。我在创建中的冥想控制器(mymeditationnotes)中不断出现未定义的错误。我不确定我的新动作是否有效。

my myitationitationnoteController

class MymeditationnotesController < ApplicationController
  before_action :find_mymeditationnote, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_listener!

  def index
    @mymeditationnotes = Mymeditationnote.all.order("created_at DESC")

  end
  def show

  end

  def new
    # @mymeditationnote = Mymeditationnote.new
    @mymeditationnote = current_listener.mymeditationnotes.build


  end

  def edit

  end

  def create

     @mymeditationnote = @current_listener.mymeditationnotes.build(mymeditationnote_params)

    if @mymeditationnote.save
      redirect_to mymeditationnotes_path
    else
      render 'new'
    end

  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def find_mymeditationnote
      @mymeditationnote = Mymeditationnote.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def mymeditationnote_params
      params.require(:mymeditationnote).permit(:title, :describe, :divide_distribute)
    end
end

我的模特

class Mymeditationnote < ActiveRecord::Base
  belongs_to :listener
end

听众模型

class Listener < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable


  has_many :listeners_speakers, class_name: 'ListenersSpeakers'
  has_many :speakers, through: :listeners_speakers
  has_many :mymeditationnotes

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

  validates_presence_of :time_zone
end

1 个答案:

答案 0 :(得分:0)

您在@current_listener行动中设置create的位置。获得nil

的原因是'mymeditationnotes' for nil:NilClass