uninitialized constant ApplicationController

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](http://amritpalpathakgne.wordpress.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

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…:stuck_out_tongue:

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

amritpalpathakgne.wordpress.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

amritpalpathakgne.wordpress.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

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…:slight_smile:

and in your routes.rb (for rails3) have

namespace :admin do

    resources :posts

end
done…:slight_smile:

still “localhost:3000/admin/posts” gives the error

Routing Error

uninitialized constant ApplicationController

amritpalpathakgne.wordpress.com

awaiting your reply sir

thanks

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

i am unable to guess reason for error…:slight_smile:

thanks

amritpalpathakgne.wordpress.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

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

Yes. Create a new directory “admin” under app/views and move “posts” directory into that one.

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](http://amritpalpathakgne.wordpress.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

problem is solved.

amritpalpathakgne.wordpress.com

Glad to help :slight_smile:

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.

amritpalpathkgne.wordpress.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

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…:stuck_out_tongue:

amritpalpathakgne.wordpress.com