簡體   English   中英

nil:NilClass的未定義方法“事件”

[英]undefined method `events' for nil:NilClass

我正在使用嵌套屬性,在提交信息並收到此錯誤之前,一切似乎都還不錯。 它說它在我的EventsController文件中:

class EventsController < ApplicationController

    def new
        @event = Event.new
        @event.songs.build
    end

    def index
        @songs = Song.all
    end

    def show
      @event = Event.find(params[:id])
      @songs = @event.songs.paginate(page: params[:page])
    end

    def create
        @event = current_user.events.build(event_params)
        if @event.save
            flash[:success] = "Event Created!"
            redirect_to user_path(@event.user)
        else
            render 'welcome#index'
        end
    end

    def destroy
    end

    private 

      def event_params
        params.require(:event).permit(:name, :partycode, song_attributes: [:event_id, :artist, :title, :genre, :partycode])
      end
end

這是我的歌曲視圖中的new.html.erb文件(根據選定事件處理歌曲提交)

<br>
<br>
<div class ="container">
  <div class="jumbotron">
  <%= form_for Event.new do |f| %>
    <h3>Enter a song:</h3>
    <%= f.fields_for :songs, Song.new do |song_form| %>

      <%= song_form.collection_select(:event_id, Event.all, :id, :name) %>
      <%= song_form.text_field :artist, placeholder: "Artist" %>
      <%= song_form.text_field :title,  placeholder: "Title" %>
      <%= song_form.text_field :genre,  placeholder: "Genre" %>
    <% end %>
    <%= link_to_add_fields "Add Song", f, :songs %>
    <%= f.text_field :partycode %>
    <%= f.submit "Submit", class: "btn btn-primary" %>
  <% end %>
  </div>
</div>

在我的ApplicationHelper.rb文件中定義了link_to_add_fields方法:

module ApplicationHelper
  def link_to_add_fields(name, f, association)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render("songs_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end
end

在session_helper.rb文件中定義了current_user:

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  def createEvent(event)
    session[:event_id] = event.id
  end



  # Returns the current logged-in user (if any).
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end
  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

  def log_out
    session.delete(:user_id)
    @current_user = nil
  end

end

最后,這是我的songs_fields文件,僅當用戶點擊顯示“添加歌曲”的鏈接時,該文件才會生成字段

<fieldset>  
  <%= f.text_field :artist, placeholder: "Artist" %>
  <%= f.text_field :title,  placeholder: "Title" %>
  <%= f.text_field :genre,  placeholder: "Genre" %>
</fieldset>

我覺得這是我在應用程序中正常工作之前的最后一部分! 因此,對此的幫助將是巨大的:D

您回答了自己的問題...如果您尚未登錄,則current_user將為nil,因此會出現此錯誤。

選項1(丑):改變你的邏輯,所以current_user.events如果CURRENT_USER是零不會被調用,而只是直接去渲染

選項2(更好):在運行操作之前,使用before_action語句強制設置current_user 取決於您用來進行身份驗證的內容,但是使用Devise時,它看起來像這樣:

class EventsController < ApplicationController
  before_action :authenticate_user!

我想也許是:

class EventsController < ApplicationController
  before_action :log_in(user)

可能會為您做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM