I'm up to my first project with ruby on rails as api, vuejs as frontend and mongodb as DB, so I'm using mongoid.
My problem comes up when I call the get method of my controller. The console error is undefined method `messages' for nil:NilClass , but when I try puts @message.inspect it gets all the messages of the room that should get, so in this point this is ok. I don't know where the error comes if before the query is well made.
message_controller.rb
class MessagesController < ApplicationController
before_action :authorize_access_request!
before_action :set_sala
def index
@message = @sala.messages
puts @message.inspect
render json: @message
end
private
def set_sala
if params[:sala_id]
@sala = Sala.find(params[:sala_id])
end
end
end
Model Message
class Message
include Mongoid::Document
include Mongoid::Timestamps::Created
field :body, type: String
belongs_to :usuario, foreign_key: :usuario_id
belongs_to :sala, foreign_key: :sala_id
end
Sala Model
class Sala
include Mongoid::Document
field :nombre, type: String
belongs_to :usuario
has_many :messages
end
Terminal
Started POST "/sala/messages" for 127.0.0.1 at 2019-11-21 15:06:01 +0100
Processing by MessagesController#index as HTML
Parameters: {"sala_id"=>"5dd66a5f575e22274d0a5578", "message"=> {"sala_id"=>"5dd66a5f575e22274d0a5578"}}
MONGODB | [55] localhost:27017 #1 | prueba2_backend_development.find | STARTED | {"find"=>"salas", "filter"=>{"_id"=>BSON::ObjectId('5dd66a5f575e22274d0a5578')}}
MONGODB | [55] localhost:27017 | prueba2_backend_development.find | SUCCEEDED | 0.001s
MONGODB | [56] localhost:27017 #1 | prueba2_backend_development.find | STARTED | {"find"=>"messages", "filter"=>{"sala_id"=>BSON::ObjectId('5dd66a5f575e22274d0a5578')}}
Started GET "/messages" for 127.0.0.1 at 2019-11-21 15:06:01 +0100
MONGODB | [56] localhost:27017 | prueba2_backend_development.find | SUCCEEDED | 0.001s
Processing by MessagesController#index as HTML
[#<Message _id: 5dd69592575e221a9e751c0d, created_at: 2019-11-21 13:48:02 UTC, body: "Mensaje 1", usuario_id: BSON::ObjectId('5dc9b447575e2237b9205299'), sala_id: BSON::ObjectId('5dd66a5f575e22274d0a5578')>, #<Message _id: 5dd695d9575e221a9e751c0e, created_at: 2019-11-21 13:49:13 UTC, body: "Sale aqui ", usuario_id: BSON::ObjectId('5dc9b447575e2237b9205299'), sala_id: BSON::ObjectId('5dd66a5f575e22274d0a5578')>]
Completed 200 OK in 8ms (Views: 0.7ms | MongoDB: 0.0ms | Allocations: 2712)
Completed 500 Internal Server Error in 2ms (MongoDB: 0.0ms | Allocations: 857)
NoMethodError (undefined method `messages' for nil:NilClass):
app/controllers/messages_controller.rb:7:in `index'
You're getting the error because @sala
is nil and you're trying to call messages
on a nil object.
If we look at your controller code, @sala
is set in set_sala
which is called before every action. Looking at your logic, if params[:sala_id]
is not there then @sala
will not be set.
Note your GET
request - Started GET "/messages" for 127.0.0.1 at 2019-11-21 15:06:01 +0100
- the param sala_id
is not present and thus the error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.