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 
On 31 March 2015 at 05:18, Jhonnatan Mc [email protected] wrote:
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.
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
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])
On Tue, Mar 31, 2015 at 8:13 AM, Jhonnatan Mc [email protected]
wrote:
^^ 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).
On 31 March 2015 at 20:21, Jhonnatan Mc [email protected] wrote:
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
On 31 March 2015 at 21:32, Jhonnatan Mc [email protected] wrote:
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.
Colin
Colin L. wrote in post #1171239:
On 31 March 2015 at 20:21, Jhonnatan Mc [email protected] wrote:
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
I tried changed the name of el file from UserService to user_service.
the class name is UserService.
In the controller I have the followings:
user = UserService.create(params[:user_params])
On 31 March 2015 at 22:16, Jhonnatan Mc [email protected] wrote:
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
On Mar 31, 2015, at 7:13 AM, Jhonnatan Mc [email protected] wrote:
@user = User.new(user_params)
end
end
Going back a few posts: there’s a pretty obvious error there, if that’s
your actual code…
Colin L. wrote in post #1171241:
On 31 March 2015 at 21:32, Jhonnatan Mc [email protected] wrote:
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.
Colin
NameError in UsersController#create
uninitialized constant UsersController::UserService
Colin L. wrote in post #1171272:
On 31 March 2015 at 22:16, Jhonnatan Mc [email protected] wrote:
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.
I got this error message:
uninitialized constant UsersController::UserService
On 1 April 2015 at 14:56, Jhonnatan Mc [email protected] wrote:
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 P. 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 L. wrote in post #1171296:
On 1 April 2015 at 14:56, Jhonnatan Mc [email protected] wrote:
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
I understood that if the class name is UserSerice the file name must be
user_service.rb, isn’t it ?
On 1 April 2015 at 15:55, Jhonnatan Mc [email protected] wrote:
It says it is “Class user_service.rb”
Colin
I corrected the name:
I am glad it is all working at last.
Colin
On 1 April 2015 at 16:47, Jhonnatan Mc [email protected] wrote:
Colin
Not yet. 
I got the same error message:
uninitialized constant UsersController::UserService
Copy/paste the rails log from start of the request showing the
complete error message and stack if any please.
Colin
Colin L. wrote in post #1171306:
On 1 April 2015 at 15:55, Jhonnatan Mc [email protected] wrote:
It says it is “Class user_service.rb”
Colin
I corrected the name:
I am glad it is all working at last.
Colin
This is working!
Thanks you so much!
Colin L. wrote in post #1171306:
On 1 April 2015 at 15:55, Jhonnatan Mc [email protected] wrote:
It says it is “Class user_service.rb”
Colin
I corrected the name:
I am glad it is all working at last.
Colin
Not yet. 
I got the same error message:
uninitialized constant UsersController::UserService.
in this line:
user = UserService.create(user_params)