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

thanks

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

On Jun 20, 2011, at 18:41 , amritpal pathak wrote:

You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
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

On Mon, Jun 20, 2011 at 12:48 PM, Tom M.
[email protected]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…:stuck_out_tongue:

def show

end

end

    end

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

  1. namespaced in source

this one follows my previous example, eg

/app/controllers/namespace/cars_controller.rb

class Namespace::CarsController < ApplicationController
end

tom

On Jun 20, 2011, at 18:54 , amritpal pathak wrote:

#def click
@post = Post.new(params[:post])

You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
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

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

On Jun 20, 2011, at 19:17 , amritpal pathak wrote:

amritpalpathakgne.wordpress.com


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
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

On Mon, Jun 20, 2011 at 1:23 PM, Tom M.
[email protected]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…:slight_smile:
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

amritpalpathakgne.wordpress.com

On Mon, Jun 20, 2011 at 1:05 PM, Tom M.
[email protected]wrote:

  1. 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??

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

On Tue, Jun 21, 2011 at 4:26 AM, Chirag S.
[email protected]wrote:

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
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

On Tue, Jun 21, 2011 at 4:28 PM, amritpal pathak

On Tue, Jun 21, 2011 at 7:06 AM, Chirag S.
[email protected]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.

On Mon, Jun 20, 2011 at 10:06 PM, amritpal pathak
<[email protected]

wrote:

class PostsController < ApplicationController <- WRONG
resources :posts
end

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

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

On Tue, Jun 21, 2011 at 4:44 PM, amritpal pathak
[email protected]wrote:

should i make a new directory in /app/views and move the “posts” directory
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Chirag
http://sumeruonrails.com

On Tue, Jun 21, 2011 at 7:21 AM, Chirag S.
[email protected]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| %>

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit %>
<%= f.label :sign %>
<%= f.text_field :sign %>
<%= f.label :value %>
<%= f.text_field :value %>
<% end %>

<%= submit_tag “Test me!”, :type => ‘button’, :onclick => ‘alert(“It
works!”)’ %>


<%= 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

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

On Tue, Jun 21, 2011 at 5:08 PM, amritpal pathak
[email protected]wrote:

ActionController::RoutingError in Admin/posts#index
<%= form_for(@post) do |f| %>

<%= submit_tag "Test me!", :type => 'button', :onclick => 'alert("It works!")'

%>

           I think we are missing something to add or else.

[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Chirag
http://sumeruonrails.com

On Tue, Jun 21, 2011 at 7:55 AM, Chirag S.
[email protected]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.

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

Glad to help :slight_smile:

On Tue, Jun 21, 2011 at 5:38 PM, amritpal pathak
[email protected]wrote:

and so on
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Chirag
http://sumeruonrails.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

On Tue, Jun 21, 2011 at 5:46 PM, amritpal pathak
[email protected]wrote:

http://groups.google.com/group/rubyonrails-talk?hl=en.


Chirag
http://sumeruonrails.com

Try this then

<%= button_to “Great”, click_admin_posts_path %>

On Tue, Jun 21, 2011 at 6:13 PM, amritpal pathak
[email protected]wrote:

[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Chirag
http://sumeruonrails.com