Object Service instance

42 views
Skip to first unread message

Jhonnatan Mc

unread,
Mar 31, 2015, 12:19:46 AM3/31/15
to rubyonra...@googlegroups.com
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 :)

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Mar 31, 2015, 4:04:05 AM3/31/15
to rubyonra...@googlegroups.com
On 31 March 2015 at 05:18, Jhonnatan Mc <li...@ruby-forum.com> 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

Jhonnatan Mc

unread,
Mar 31, 2015, 9:13:59 AM3/31/15
to rubyonra...@googlegroups.com
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])

tamouse pontiki

unread,
Mar 31, 2015, 10:07:08 AM3/31/15
to rubyonra...@googlegroups.com
On Tue, Mar 31, 2015 at 8:13 AM, Jhonnatan Mc <li...@ruby-forum.com> wrote:
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).
 

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])
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0698186294f783829b9056762b8840db%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Jhonnatan Mc

unread,
Mar 31, 2015, 3:22:10 PM3/31/15
to rubyonra...@googlegroups.com

Colin Law

unread,
Mar 31, 2015, 4:25:25 PM3/31/15
to rubyonra...@googlegroups.com
On 31 March 2015 at 20:21, Jhonnatan Mc <li...@ruby-forum.com> 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

Jhonnatan Mc

unread,
Mar 31, 2015, 4:32:36 PM3/31/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1171239:
I tried changed the name of el file from UserService to user_service.
the class name is UserService.

Colin Law

unread,
Mar 31, 2015, 4:43:00 PM3/31/15
to rubyonra...@googlegroups.com
Correct the code in your repo then copy/paste here the error you get
with the correct name.

Colin

Jhonnatan Mc

unread,
Mar 31, 2015, 5:16:55 PM3/31/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1171241:
NameError in UsersController#create
uninitialized constant UsersController::UserService

Colin Law

unread,
Apr 1, 2015, 3:27:18 AM4/1/15
to rubyonra...@googlegroups.com
On 31 March 2015 at 22:16, Jhonnatan Mc <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1171241:
>> On 31 March 2015 at 21:32, Jhonnatan Mc <li...@ruby-forum.com> 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.

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

> NameError in UsersController#create
> uninitialized constant UsersController::UserService
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8c48b7f0eadba1f036b367cce32aff6%40ruby-forum.com.

Jhonnatan Mc

unread,
Apr 1, 2015, 9:57:10 AM4/1/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1171272:
> On 31 March 2015 at 22:16, Jhonnatan Mc <li...@ruby-forum.com> 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:

Scott Ribe

unread,
Apr 1, 2015, 10:11:17 AM4/1/15
to rubyonra...@googlegroups.com, Jhonnatan Mc
On Mar 31, 2015, at 7:13 AM, Jhonnatan Mc <li...@ruby-forum.com> wrote:
>
> #App/services/UserService.rb
>
> class UserService
>
> class UserService
>
>
> def self.create(user_params)
>
> @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...

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





Colin Law

unread,
Apr 1, 2015, 10:18:30 AM4/1/15
to rubyonra...@googlegroups.com
On 1 April 2015 at 14:56, Jhonnatan Mc <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1171272:
>> On 31 March 2015 at 22:16, Jhonnatan Mc <li...@ruby-forum.com> 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.

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 got this error message:
> uninitialized constant UsersController::UserService
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3209bb7eadc911c0c8f690d06d5e5286%40ruby-forum.com.

Brad Pauly

unread,
Apr 1, 2015, 10:37:58 AM4/1/15
to rubyonra...@googlegroups.com
You'll also need to make sure app/services is in autoload_paths in application.rb

Jhonnatan Mc

unread,
Apr 1, 2015, 10:54:30 AM4/1/15
to rubyonra...@googlegroups.com
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 )

Jhonnatan Mc

unread,
Apr 1, 2015, 10:56:07 AM4/1/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1171296:
> On 1 April 2015 at 14:56, Jhonnatan Mc <li...@ruby-forum.com> 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

Colin Law

unread,
Apr 1, 2015, 11:25:21 AM4/1/15
to rubyonra...@googlegroups.com
On 1 April 2015 at 15:55, Jhonnatan Mc <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1171296:
>> On 1 April 2015 at 14:56, Jhonnatan Mc <li...@ruby-forum.com> 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:

I am glad it is all working at last.

Colin

>
> #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
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ca5b89f527d91b14cd559e1e5ad51f5e%40ruby-forum.com.

Jhonnatan Mc

unread,
Apr 1, 2015, 11:47:56 AM4/1/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1171306:
> On 1 April 2015 at 15:55, Jhonnatan Mc <li...@ruby-forum.com> 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

Colin Law

unread,
Apr 1, 2015, 11:54:44 AM4/1/15
to rubyonra...@googlegroups.com
On 1 April 2015 at 16:47, Jhonnatan Mc <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1171306:
>> On 1 April 2015 at 15:55, Jhonnatan Mc <li...@ruby-forum.com> 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

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

Colin

Jhonnatan Mc

unread,
Apr 1, 2015, 11:58:29 AM4/1/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1171306:
> On 1 April 2015 at 15:55, Jhonnatan Mc <li...@ruby-forum.com> 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!
Reply all
Reply to author
Forward
0 new messages