uninitialized constant ApplicationController

1,306 views
Skip to first unread message

amritpal pathak

unread,
Jun 20, 2011, 12:41:23 PM6/20/11
to rubyonra...@googlegroups.com
i am following the routing guides (2.6) on rails 3.According to it, i combined the 2 controller under namespace "admin"  but couldn't success to make it work.Before doing so everything was working at "localhost:3000/posts".
I just made a new directory "admin" in  /app/controller/admin and then added 2 controller name "posts_controller.rb" and "gne_controller.rb" inside it.
 but when i browsed to "localhost:3000/admin/posts" ,it gave error

uninitialized constant ApplicationController
please help to solve it and understand..:)
thanks
amritpalpathakgne.wordpress.com

Tom Meinlschmidt

unread,
Jun 20, 2011, 12:48:53 PM6/20/11
to rubyonra...@googlegroups.com
show us the source of one controller at least

there should be in app/controllers/admin/posts_controller.rb

class Admin::PostController < ApplicationController
end

tom

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
===============================================================================

amritpal pathak

unread,
Jun 20, 2011, 12:54:16 PM6/20/11
to rubyonra...@googlegroups.com
On Mon, Jun 20, 2011 at 12:48 PM, Tom Meinlschmidt <to...@meinlschmidt.com> wrote:
show us the source of one controller at least
ya sure.

there should be in app/controllers/admin/posts_controller.rb
   it is as follow and it was working without using namespace i.e when it was in /app/controller.
    it is posts_controller.rb..:P
     
class PostsController < ApplicationController
#def click
#end  
def new 
end
def show
  @show = Post.find(params[:id])
  end
  def index
  @post = Post.new
  end
 def create
  @post = Post.new(params[:post])
  respond_to do |format|
  if @post.save
  format.html{ redirect_to(@post, :notice => 'Post   was successfully created.') }
end
end
        end
 end

Tom Meinlschmidt

unread,
Jun 20, 2011, 1:05:00 PM6/20/11
to rubyonra...@googlegroups.com
sure.. when you want to use namespace, having two options

1. "pseudo" namespace from routes

in routes.rb

map '/namespace/:controller/:action/:id'

and then you'll have all the controllers in /app/controllers w/o any namespace in class name

2. namespaced in source

this one follows my previous example, eg

/app/controllers/namespace/cars_controller.rb

class Namespace::CarsController < ApplicationController
end

tom

amritpal pathak

unread,
Jun 20, 2011, 1:17:30 PM6/20/11
to rubyonra...@googlegroups.com
On Mon, Jun 20, 2011 at 1:05 PM, Tom Meinlschmidt <to...@meinlschmidt.com> wrote:
sure.. when you want to use namespace, having two options

1. "pseudo" namespace from routes

in routes.rb

map '/namespace/:controller/:action/:id'

and then you'll have all the controllers in /app/controllers w/o any namespace in class name

2. namespaced in source

this one follows my previous example, eg

/app/controllers/namespace/cars_controller.rb

Thats what i am doing.Call you please tell me why error exists??
   

Tom Meinlschmidt

unread,
Jun 20, 2011, 1:23:09 PM6/20/11
to rubyonra...@googlegroups.com
I said...

if you have posts_controller in /app/controllers/admin/posts_controller.rb

then you MUST have this namespace in your class name

class PostsController < ApplicationController <- WRONG

class Admin::PostsController < ApplicationController <- RIGHT

and in your routes.rb (for rails3) have

namespace :admin do
resources :posts
end

tom

amritpal pathak

unread,
Jun 20, 2011, 10:06:30 PM6/20/11
to rubyonra...@googlegroups.com
On Mon, Jun 20, 2011 at 1:23 PM, Tom Meinlschmidt <to...@meinlschmidt.com> wrote:
I said...

if you have posts_controller in /app/controllers/admin/posts_controller.rb

then you MUST have this namespace in your class name

class PostsController < ApplicationController <- WRONG
     ok  

class Admin::PostsController < ApplicationController <- RIGHT
  done..:)

and in your routes.rb (for rails3) have

namespace :admin do
       resources :posts
end  
      done.....:) 
         still "localhost:3000/admin/posts" gives the error
         Routing Error
       uninitialized constant ApplicationController

amritpal pathak

unread,
Jun 21, 2011, 12:30:08 AM6/21/11
to rubyonra...@googlegroups.com
On Mon, Jun 20, 2011 at 10:06 PM, amritpal pathak <amritpa...@gmail.com> wrote:


On Mon, Jun 20, 2011 at 1:23 PM, Tom Meinlschmidt <to...@meinlschmidt.com> wrote:
I said...

if you have posts_controller in /app/controllers/admin/posts_controller.rb

then you MUST have this namespace in your class name

class PostsController < ApplicationController <- WRONG
     ok  

class Admin::PostsController < ApplicationController <- RIGHT
  done..:)

and in your routes.rb (for rails3) have

namespace :admin do
       resources :posts
end  
      done.....:) 
         still "localhost:3000/admin/posts" gives the error
         Routing Error
        
           awaiting   your reply sir
     thanks 

Chirag Singhal

unread,
Jun 21, 2011, 4:26:12 AM6/21/11
to rubyonra...@googlegroups.com
Hi Amrit,

Are you by any chance migrating this project from Rails 2.x to 3.x?
If yes, you may have the application controller in old format.

Can you check and verify if you have a file app/controllers/application_controller.rb?
If you don't have that, then you might have app/controllers/application.rb, in that case you will need to rename it to application_controller.rb

Hope this helps

amritpal pathak

unread,
Jun 21, 2011, 6:58:11 AM6/21/11
to rubyonra...@googlegroups.com
sir this file was in app/controller/application_controller.rb before using namespacing.Now i hadcopied it to app/view/admin  along with posts_controller.rb

    i am unable to guess reason for error..:)

   thanks 

Chirag Singhal

unread,
Jun 21, 2011, 7:06:00 AM6/21/11
to rubyonra...@googlegroups.com
That's the problem. Don't move application_controller.rb file
It should always be under app/controllers folder not in the namespaced folder.
Move it back and your error should go away


--

amritpal pathak

unread,
Jun 21, 2011, 7:14:27 AM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 7:06 AM, Chirag Singhal <chirag....@gmail.com> wrote:
That's the problem. Don't move application_controller.rb file
It should always be under app/controllers folder not in the namespaced folder.
Move it back and your error should go away
  thanks sir .Thats error is solved but another one came w hen i browse to "localhost:3000/admin/posts".
    
    

Template is missing

Missing template admin/posts/index with {:handlers=>[:rhtml, :rxml, :erb, :builder, :rjs], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/amrit/check/app/views"

The index.html.erb file exist already in /app/view/posts/index.html.erb.

should i make a new directory in /app/views and  move the "posts" directory inside it same as namespacing in controller??

Thank you very much for support.

amritpalpatakgne.wordpress.com 

Chirag Singhal

unread,
Jun 21, 2011, 7:21:37 AM6/21/11
to rubyonra...@googlegroups.com
Yes. Create a new directory "admin" under app/views and move "posts" directory into that one.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

amritpal pathak

unread,
Jun 21, 2011, 7:38:07 AM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 7:21 AM, Chirag Singhal <chirag....@gmail.com> wrote:
Yes. Create a new directory "admin" under app/views and move "posts" directory into that one.
again bad luck.I did as you said.
new error
    

ActionController::RoutingError in Admin/posts#index

Showing /home/amrit/check/app/views/admin/posts/index.html.erb where line #2 raised:

No route matches {:controller=>"posts", :action=>"new"}
I did everything as previous before namespacing ,it was working.The file new.html.erb exist in same directory.This file looks :

<%= link_to 'New Post', new_post_path%>
<%= form_for(@post) do |f| %>
 

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>

<div align="center">

  <div class="actions">
    <%= f.submit %>
  </div>
</div>
<div class="field">
    <%= f.label :sign %><br />
    <%= f.text_field :sign %>
  </div>
<div class="field">
    <%= f.label :value %><br />
    <%= f.text_field :value %>
   
  </div>
<% end %>

<%= submit_tag "Test me!", :type => 'button', :onclick => 'alert("It works!")' %>
<br>
<%= submit_tag "Click me", :type => 'button' %>
<%= button_to "Great", :action => "click"%>
 <%= button_to "click me", :action => "work"%>

        here if i removed 1st time just to test ,the next line gives the error:
No route matches {:controller=>"posts"}
               I think we are missing something to add or else. 
thanks 
amritpalpathakgne.wordpress.com

Chirag Singhal

unread,
Jun 21, 2011, 7:55:02 AM6/21/11
to rubyonra...@googlegroups.com
All your routes will need to change as per the named scope

new_post_path should be changed to new_admin_post_path

form_for(@post) should be changed to form_for [:admin, @post]

and so on

Check rake:routes for the full list of new routes with the namescope



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

amritpal pathak

unread,
Jun 21, 2011, 8:08:11 AM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 7:55 AM, Chirag Singhal <chirag....@gmail.com> wrote:
All your routes will need to change as per the named scope

new_post_path should be changed to new_admin_post_path

form_for(@post) should be changed to form_for [:admin, @post]

and so on

thank you very much sir.
    problem is solved.

amritpal pathak

unread,
Jun 21, 2011, 8:16:11 AM6/21/11
to rubyonra...@googlegroups.com

Last query.what should be the syntax to button's part .It was
  <%= button_to "Great", :action => "click"%>
  So now it says

 No route matches {:controller=>"admin/posts", :action=>"click"}
i tried to change it too as:
<%= button_to "Great", :action => "admin_click"%>

But didn't work.

Chirag Singhal

unread,
Jun 21, 2011, 8:16:11 AM6/21/11
to rubyonra...@googlegroups.com
Glad to help :)

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.



--
Chirag

Chirag Singhal

unread,
Jun 21, 2011, 8:27:02 AM6/21/11
to rubyonra...@googlegroups.com
In which controller do you have the "click" action defined?
If it's in the same controller, then you need to define it in your routes too.

namespace :admin do
  resources :posts, :collection => {:click => :post}
end

And in the code you had posted, the click method in your controller was commented, so that may be the issue as well


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

amritpal pathak

unread,
Jun 21, 2011, 8:43:27 AM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 8:27 AM, Chirag Singhal <chirag....@gmail.com> wrote:
In which controller do you have the "click" action defined?
If it's in the same controller
      yes it is in the same 
then you need to define it in your routes too.

namespace :admin do
  resources :posts, :collection => {:click => :post}
end
Did exactly
And in the code you had posted, the click method in your controller was commented, so that may be the issue as well
   uncommented it.

  still   no effect..:P
 



Chirag Singhal

unread,
Jun 21, 2011, 8:52:36 AM6/21/11
to rubyonra...@googlegroups.com
Try this then

<%= button_to "Great", click_admin_posts_path %>


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

amritpal pathak

unread,
Jun 21, 2011, 8:56:53 AM6/21/11
to rubyonra...@googlegroups.com


On Tue, Jun 21, 2011 at 8:52 AM, Chirag Singhal <chirag....@gmail.com> wrote:
Try this then
 no it haven't work too.
    
    undefined local variable or method `click_admin_posts_path' for #<#<Class:0xb6675348>:0xb66732a0>


Chirag Singhal

unread,
Jun 21, 2011, 9:09:40 AM6/21/11
to rubyonra...@googlegroups.com
Can you paste the code from your routes file here?


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

amritpal pathak

unread,
Jun 21, 2011, 9:16:08 AM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 9:09 AM, Chirag Singhal <chirag....@gmail.com> wrote:
Can you paste the code from your routes file here?


Check::Application.routes.draw do
 # get "gne/clg"

  get "posts/index"
#resources :posts, :module => "admin"
namespace "admin" do
resources :posts ,:collection =>{:click => :post}
end
 #working right 
#resources :posts do
#end
#working right
root :to => "posts#click"
root :to => "posts#work"
#get 'posts/click'
#ActionController::Routing::Routes.draw do |map|
#map.root :controller => "posts", :action => "click"
#end
#ActionController::Routing::Routes.draw do |map|
#map.root :controller => "posts", :action => "work"
#end
end

Colin Law

unread,
Jun 21, 2011, 9:21:35 AM6/21/11
to rubyonra...@googlegroups.com
On 21 June 2011 14:16, amritpal pathak <amritpa...@gmail.com> wrote:
>
>
> On Tue, Jun 21, 2011 at 9:09 AM, Chirag Singhal <chirag....@gmail.com>
> wrote:
>>
>> Can you paste the code from your routes file here?
>>
>> Check::Application.routes.draw do
>>  # get "gne/clg"
>>   get "posts/index"
>> #resources :posts, :module => "admin"
>> namespace "admin" do
>> resources :posts ,:collection =>{:click => :post}
>> end
>>  #working right
>> #resources :posts do
>> #end
>> #working right
>> root :to => "posts#click"
>> root :to => "posts#work"

Can you explain what these two routes are doing?

Colin

amritpal pathak

unread,
Jun 21, 2011, 9:34:35 AM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 9:21 AM, Colin Law <cla...@googlemail.com> wrote:
On 21 June 2011 14:16, amritpal pathak <amritpa...@gmail.com> wrote:
>
>
> On Tue, Jun 21, 2011 at 9:09 AM, Chirag Singhal <chirag....@gmail.com>
> wrote:
>>
>> Can you paste the code from your routes file here?
>>
>> Check::Application.routes.draw do
>>  # get "gne/clg"
>>   get "posts/index"
>> #resources :posts, :module => "admin"
>> namespace "admin" do
>> resources :posts ,:collection =>{:click => :post}
>> end
>>  #working right
>> #resources :posts do
>> #end
>> #working right
>> root :to => "posts#click"
>> root :to => "posts#work"

Can you explain what these two routes are doing?
        These 2 routes were for "Great" and "click me" buttton before using namespacing. 

 should the changes be in these 2 routes for namespacing rather than as:

 namespace "admin" do
resources :posts ,:collection =>{:click => :post}
end

Chirag Singhal

unread,
Jun 21, 2011, 9:42:01 AM6/21/11
to rubyonra...@googlegroups.com
Sorry, I sent you the rails 2.3 way of routing.
It should have been like this:

namespace "admin" do
  resources :posts do
    collection do
      post :click
  end
end

And as Colin said, you can't have two "root" routes for your app. Keep one of them and remove the other one.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Chirag Singhal

unread,
Jun 21, 2011, 9:44:57 AM6/21/11
to rubyonra...@googlegroups.com
Also, you may want to read up more on routing.
Here's a link to official guide, which does a great job of explaining how routing works in rails 3


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Colin Law

unread,
Jun 21, 2011, 9:59:27 AM6/21/11
to rubyonra...@googlegroups.com
On 21 June 2011 14:44, Chirag Singhal <chirag....@gmail.com> wrote:
> Also, you may want to read up more on routing.
> Here's a link to official guide, which does a great job of explaining how
> routing works in rails 3
> http://guides.rubyonrails.org/routing.html

I have lost track of how many times Aritpal Pathak has been advised to
read that. Also you might like to try getting him to post the result
of rake routes.

Colin

Michael Pavling

unread,
Jun 21, 2011, 10:31:11 AM6/21/11
to rubyonra...@googlegroups.com
On 21 June 2011 14:59, Colin Law <cla...@googlemail.com> wrote:
> Also you might like to try getting him to post the result
> of rake routes.

ROFLMAO :-)

amritpal pathak

unread,
Jun 21, 2011, 12:00:25 PM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 9:42 AM, Chirag Singhal <chirag....@gmail.com> wrote:
Sorry, I sent you the rails 2.3 way of routing.
It should have been like this:

namespace "admin" do
  resources :posts do
    collection do
      post :click
  end
end
      Great!!
      working now.Thank you sir
     

And as Colin said, you can't have two "root" routes for your app. Keep one of them and remove the other one.

amritpal pathak

unread,
Jun 21, 2011, 12:01:28 PM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 9:44 AM, Chirag Singhal <chirag....@gmail.com> wrote:
Also, you may want to read up more on routing.
Here's a link to official guide, which does a great job of explaining how routing works in rails 3


thats what i am following  and i got this error from this tutorial.BTW thanks again.......:P

amritpal pathak

unread,
Jun 21, 2011, 12:06:03 PM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 9:59 AM, Colin Law <cla...@googlemail.com> wrote:
On 21 June 2011 14:44, Chirag Singhal <chirag....@gmail.com> wrote:
> Also, you may want to read up more on routing.
> Here's a link to official guide, which does a great job of explaining how
> routing works in rails 3
> http://guides.rubyonrails.org/routing.html

I have lost track of how many times Aritpal Pathak has been advised to
read that. 
   Sir this time i am your advice and reading it .i got the error while  following section number 2.6 on namespacing.Thats why i posted here and now got my answer.Now will move ahead on tutorial.
 Again thanks to all

Colin Law

unread,
Jun 21, 2011, 12:18:47 PM6/21/11
to rubyonra...@googlegroups.com

That is good. You could try and help Joanne(Yennie) with her problems
if you wished to make a contribution back to the Rails Community.

Colin

amritpal pathak

unread,
Jun 21, 2011, 12:22:29 PM6/21/11
to rubyonra...@googlegroups.com
On Tue, Jun 21, 2011 at 12:18 PM, Colin Law <cla...@googlemail.com> wrote:
On 21 June 2011 17:06, amritpal pathak <amritpa...@gmail.com> wrote:
>
>
> On Tue, Jun 21, 2011 at 9:59 AM, Colin Law <cla...@googlemail.com> wrote:
>>
>> On 21 June 2011 14:44, Chirag Singhal <chirag....@gmail.com> wrote:
>> > Also, you may want to read up more on routing.
>> > Here's a link to official guide, which does a great job of explaining
>> > how
>> > routing works in rails 3
>> > http://guides.rubyonrails.org/routing.html
>>
>> I have lost track of how many times Aritpal Pathak has been advised to
>> read that.
>
>    Sir this time i am your advice and reading it .i got the error while
>  following section number 2.6 on namespacing.Thats why i posted here and now
> got my answer.Now will move ahead on tutorial.
> Again thanks to all

That is good.  
     Thank you sir.i could be motivated just because of you otherwise i was going to quit RoR. 
You could try and help Joanne(Yennie) with her problems
if you wished to make a contribution back to the Rails Community.
   Why not!!
   is there any mail from her side?

Colin Law

unread,
Jun 21, 2011, 12:35:11 PM6/21/11
to rubyonra...@googlegroups.com

Have you not seen her postings? One of the best ways to learn about a
subject is to read all the threads on the forum and for any questions
where you understand the question then make sure you understand the
answers. In no time you will find yourself answering questions. Her
thread subject is "Error of undefined method".

Colin

amritpal pathak

unread,
Jun 21, 2011, 12:38:38 PM6/21/11
to rubyonra...@googlegroups.com
       ok will follow it. 
and for any questions
where you understand the question then make sure you understand the
answers.  In no time you will find yourself answering questions.  Her
thread subject is "Error of undefined method".
Reply all
Reply to author
Forward
0 new messages