我正在爲我的一個項目從頭開始構建消息傳遞系統。 一切工作正常,但我只是有以下錯誤:NoMethodError in MessagesController#index
其次undefined method 'length' for nil:NilClass
。 不幸的是,我無法找到與我的案例相對應的答案。Rails4動作控制器 - 未定義的方法'長度'爲零:NilClass
我給你conversations_controller.rb
,messages_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)
我希望這將是對你有所幫助,我們將能夠找到答案:)
乾杯
您還沒有初始化'@之前messages'在'index'行動'if @ messages.length> 10' ...這就是爲什麼'@ messages'在您嘗試調用'length'方法時爲零的原因。 –