Object Service instance

Hi (I am a beginner)

I have a project that add users, profiles, etc.
I genetered it using scaffold. It's everything ok up here.
I need to do the crud operations in a service class and not in the
controllers.

I created a services folder into App folder and create a UserService.rb
class .

So the idea is that the controller call to UserService class , and the
userService Class do the crud(create, read, update, delete) operation.

But I got this error message : uninitialized constant
UsersController::UserService .

I tried add a instance to routes.rb , also to application.rb but the
error is still there.

I have searched many examples and tried many things, but can not find
solution.

Any suggestions?
PD: Sorry for my English :slight_smile:

Why?

Colin

I don't know.

I'am trying to do this:

#App/Controller/user_controller
def create
      @user = UserService.create(params[:user_params])
  end

#App/services/UserService.rb

class UserService

class UserService

   def self.create(user_params)

     @user = User.new(user_params)

  end
end

I got this error message:
uninitialized constant UsersController::UserService

@user = UserService.create(params[:user_params])

I don't know.

I'am trying to do this:

#App/Controller/user_controller
def create
      @user = UserService.create(params[:user_params])
  end

#App/services/UserService.rb

^^ If this is the *actual* name of the file, then Rails autoloading will
not work on it. When Rails encounters a class called "UserService" it looks
through the autoload paths for a file entitled: "user_service.rb" (i.e.
snake case, not CamelCase).

I tried that.
this is the project:

https://github.com/JhonnatanMc/Object_Service_RoR/tree/master/prueba

I tried that.
this is the project:

You tried what? You have not quoted the previous message so we don't
know what you are referring to.

https://github.com/JhonnatanMc/Object_Service_RoR/tree/master/prueba

Look at the name of the class (UserService). Now look at the name of
the file. Now look again if necessary. Keep looking...

Colin

Colin Law wrote in post #1171239:

Correct the code in your repo then copy/paste here the error you get
with the correct name.

Colin

Colin Law wrote in post #1171241:

Colin Law wrote in post #1171241:

Look at the name of the class (UserService). Now look at the name of
the file. Now look again if necessary. Keep looking...

Colin

I tried changed the name of el file from UserService to user_service.
the class name is UserService.

Correct the code in your repo then copy/paste here the error you get
with the correct name.

The code is still wrong in the repo. As I said, correct the code in
the repo, make sure that is the code you are running, and try again.
It is difficult to help if you do not do what is asked.

Colin

You’ll also need to make sure app/services is in autoload_paths in application.rb

https://github.com/JhonnatanMc/Object_Service_RoR/blob/master/prueba/config/application.rb#L14

Colin Law wrote in post #1171272:

Going back a few posts: there's a pretty obvious error there, if that's your actual code...

Colin Law wrote in post #1171272:

Correct the code in your repo then copy/paste here the error you get
with the correct name.

The code is still wrong in the repo. As I said, correct the code in
the repo, make sure that is the code you are running, and try again.
It is difficult to help if you do not do what is asked.

Colin

I corrected the code in the repo. The file name is user_service.rb and
the class name is : UserService.

That is not what this says the file name is
https://github.com/JhonnatanMc/Object_Service_RoR/tree/master/prueba/app/services
It says it is "Class user_service.rb"

Colin

Brad Pauly wrote in post #1171299:

You'll also need to make sure app/services is in autoload_paths in
application.rb

https://github.com/JhonnatanMc/Object_Service_RoR/blob/master/prueba/config/application.rb#L14

In application.rb I wrote this line:

config.autoload_paths += %W( #{config.root}/app/services )

Colin Law wrote in post #1171296:

Colin

I corrected the code in the repo. The file name is user_service.rb and
the class name is : UserService.

That is not what this says the file name is

https://github.com/JhonnatanMc/Object_Service_RoR/tree/master/prueba/app/services

It says it is "Class user_service.rb"

Colin

I corrected the name:

#App/Controller/user_controller
def create
      @user = UserService.create(user_params)
  end

#App/services/user_service.rb

class UserService

   def self.create(user_params)

     @user = User.new(user_params)
       ...
  end
end

Colin Law wrote in post #1171296:

Colin

I corrected the code in the repo. The file name is user_service.rb and
the class name is : UserService.

That is not what this says the file name is

https://github.com/JhonnatanMc/Object_Service_RoR/tree/master/prueba/app/services

It says it is "Class user_service.rb"

Colin

I corrected the name:

I am glad it is all working at last.

Colin

Colin Law wrote in post #1171306:

Copy/paste the rails log from start of the request showing the
complete error message and stack if any please.

Colin

Colin Law wrote in post #1171306: