2016-11-13 34 views
0

我正在爲我的一個項目從頭開始構建消息傳遞系統。 一切工作正常,但我只是有以下錯誤:NoMethodError in MessagesController#index其次undefined method 'length' for nil:NilClass。 不幸的是,我無法找到與我的案例相對應的答案。Rails4動作控制器 - 未定義的方法'長度'爲零:NilClass

我給你conversations_controller.rbmessages_controller.rb,模型和路由(和日誌):

conversations_controller.rb

class ConversationsController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @users = User.all 
    @conversations = Conversation.all 
    end 

    def create 
    if Conversation.between(params[:sender_id], params[:recipient_id]).present? 
     @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first 
    else 
     @conversation = Conversation.create!(conversation_params) 
    end 
    redirect_to conversation_messages_path(@conversation) 
    end 

    private 

    def conversation_params 
    params.permit(:sender_id, :recipient_id) 
    end 

end 

messages_controller.rb:

class MessagesController < ApplicationController 

    def index 
    if @messages.length > 10 
     @over_ten = true 
     @messages = @messages[-10..-1] 
    end 
    if params[:m] 
     @over_ten = false 
     @messages = @conversation.messages 
    end 
    if @messages.last 
     if @messages.last.user_id != current_user.id 
     @messages.last.read = true; 
     end 
    end 
    @message = @conversation.messages.new 
    end 

    def new 
    @message = @conversation.messages.new 
    end 

    def create 
    @message = @conversation.messages.new(message_params) 
    if @message.save 
     redirect_to conversation_messages_path(@conversation) 
    end 
    end 

    private 

    def message_params 
    params.require(:message).permit(:body, :user_id) 
    end 
end 

conversation.rb:

class Conversation < ActiveRecord::Base 
    belongs_to :sender, foreign_key: :sender_id, class_name: 'User' 
    belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User' 

    has_many :messages, dependent: :destroy 

    validates_uniqueness_of :sender_id, scope: :recipient_id 

    scope :between, -> (sender_id,recipient_id) do 
    where("(conversations.sender_id = ? AND conversations.recipient_id = ?) 
      OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", 
      sender_id,recipient_id, recipient_id,sender_id) 
    end 
end 

message.rb:

class Message < ActiveRecord::Base 
    belongs_to :conversation 
    belongs_to :user 

    validates_presence_of :body, :conversation_id, :user_id 

    def message_time 
    created_at.strftime("%m/%d/%y at %l:%M %p") 
    end 
end 

的routes.rb:

Rails.application.routes.draw do 

    get 'messages/index' 

    get 'messages/new' 

    get 'conversations/index' 

    get 'dashboard/index' 

    get 'dashboard/trips' 

    get 'dashboard/messages' 

    get "welcome/index" 

    resources :conversations do 
    resources :messages 
    end 

    devise_for :users 

    authenticated :users do 
    root to: 'dashboard#index', as: :authenticated_root 
    end 

    root "welcome#index" 
end 

這裏,日誌:

Started GET "/conversations/5/messages" for ::1 at 2016-11-13 16:45:38 +1030 
Processing by MessagesController#index as HTML 
    Parameters: {"conversation_id"=>"5"} 
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms) 

NoMethodError (undefined method `length' for nil:NilClass): 
    app/controllers/messages_controller.rb:4:in `index' 


    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.7ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (53.1ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (0.5ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.3ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (40.4ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.7ms) 
    Rendered /Users/JUSTINE/.rvm/gems/ruby-2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (91.6ms) 

我希望這將是對你有所幫助,我們將能夠找到答案:)

乾杯

+0

您還沒有初始化'@之前messages'在'index'行動'if @ messages.length> 10' ...這就是爲什麼'@ messages'在您嘗試調用'length'方法時爲零的原因。 –

回答

0

您的MessagesController中的實例變量@messages未設置。你需要先初始化它像current_user.messages:當你在格式紅寶石看到任何錯誤消息

def index 
    @messages = current_user.messages # or something like this 

    if @messages.length > 10 
     @over_ten = true 
     # ... 
0

undefined method 'method_name' for nil:NilClass 

你被告知你想叫的東西在零對象,所以你的注意力的焦點應該是爲什麼該對象爲零。您還告訴記者,在您的記錄發生錯誤:

app/controllers/messages_controller.rb:4:in `index' 

這是這一行:

if @messages.length > 10 

所以@messages是零,在這種情況下,它是零,因爲它實際上並沒有宣佈:

之前,如果塊做類似:

@messages = Message.all 
+0

它的工作,但現在我有另一個錯誤信息是'找不到用戶與'id'= 0' – justinedps26

+0

那麼你應該考慮發佈另一個問題,因爲我認爲這是完全不同的問題,並不容易告訴什麼問題是沒有相關信息。 – David

+0

我發現了這個問題,謝謝你的幫助:) – justinedps26

相關問題