When I hit this link:
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
I get this error:
uninitialized constant InvitationsController
The method in InvitationsController looks like this:
def sumbit_invitation
#invi = #invitations.find(params[:id])
#invi.update_attributes(accepted: true)
end
and routes is:
resources :invitations do
put :sumbit_invitation
end
I propose you can follow the below steps to resolve your problem:
1/ run command rake routes to make sure your routes are correct?
In my opinion, it should be:
resources :invitations do
member do
put :sumbit_invitation
end
end
2/ Make sure you URL is correct
link_to("Accept", invitation_sumbit_invitation_url(invitations), method: :put)
should be changed to
link_to("Accept", invitations_sumbit_invitation_path(invitations), method: :put)
Using rake routes to get correct path
4/ Make sure that your controller InvitationsController worked
P/S: It is better if you share your routes.rb file or logs rake routes
You controller is in the module ControlPanel and lives in the control_panel subfolder of your app's controller's folder, but your route doesn't have a corresponding controll_panel namespace defined.
Add a controll_panel namespace to your routes:
namespace :controll_panel do
resources :invitations do
put :sumbit_invitation
end
end
This will make the invitation_sumbit_invitation_url invalid and you will have to change it to something like admin_invitation_sumbit_invitation_url or invitation_sumbit_admin_invitation_url. From the top of my head I am not sure how Rails handles the naming. Use rake routes in your console to get a list of all available route names in your app.
I this context I advise to read: The Rails Guide: Routing
Related
I'm new to rails and I'm trying to create a new route to perform an action.
Currently I have the following defined in my routes
match 'books/scrape_data' => 'books#scrape_data', :via => :get
and the following action
def scrape_data
#books = Book.all
#books.each do |book|
# do stuff
end
redirect_to :action => 'list'
end
But the route seems to go to the show action with :id 'scrape_data'.
Can anyone point my in the right direction?
It sounds like you must also have a route definition such as resources :books that appears before the route you listed in your question. Since routes are parsed top-down, the books#show route is intercepting your route and bucketing scrape_data into the id. Run bundle exec rake routes to see the order of the routes defined.
The easy fix is to move your route up above resources :books. But it'd actually be more correct to get rid of your custom route and add it to the resources block like this:
resources :books do
get 'scrape_data', on: :collection
end
After this, do another bundle exec rake routes and see if that's what you wanted.
Excuse the "answer", but I don't have the rep to comment.
My go to resource for all things routing related is Routing from the outside in
Run bundle exec rake routes to see your routes and how they're nested. Then you'll see whether your params are matching up (common mistake with nested routes).
You can also view all of your routes by going to:
http://localhost:3000/rails/info/routes
which can be pretty helpful instead of having to kill your server and use the console.
I have an AppleController
it has a def sliceme method
when I go to: /apple#sliceme
it routes to #index
In my routes.config I have
resources :apples
Why?? And what is the correct route??
Resources will create the CRUD method routes (see here)
If you want to specificity another route you can specify it like so in your routes file:
get "apple/sliceme", to: "apple#sliceme"
Or
resources :apple do
get :sliceme, on: :collection
end
To check what routes actually exist, run rake routes in the terminal
I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
namespace namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin. That's what admin/admin#index means. :)
According to rubyonrails.org guide on routing you should be able to do something like this.
it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
on rails 4 in routes two roots not permited now
use
get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index and other methods to be in straightforward AdminController controller in usual directory, views to go in views/admin etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end
I could not fix this in Rails 3.2.12, maybe I am missing something.
config/routes.rb
get "home/index"
root :to => "home#index"
devise_for :users, :only => :omniauth_callbacks
match 'users/auth/:provider/callback' => 'authentications#create'
match '/auth/:provider/signout' => 'authentications#signout'
app/controllers/authentication_controller.rb
class AuthenticationsController < ApplicationController
...
end
app/models/authentication.rb
class Authentication < ActiveRecord::Base
...
end
I think it should work with my current knowledge, but there is something that I miss.
My kind question would be to tell what is wrong, please.
Rounting Error
uninitialized constant AuthenticationsController
This is a message that shows up at http://localhost:3000/auth/facebook/signout
Rails requires the file name to match the class name. Therefore you should rename app/controllers/authentication_controller.rb to app/controllers/authentications_controller.rb.
Though this question has been answered, I found another case where I was getting this error and wanted to document it here for posterity.
If you have two similar routes defined in your routes.rb file without the corresponding controllers you will get the uninitialized constant error.
Steps to reproduce:
rails generate scaffold foobar name:string
bundle exec rake db:migrate
add resources :foobars to routes.rb to a new scope (note: the foobars resource was already automatically added to the top of your routes.rb during scaffold generation) like this:
resources :foobars
########################################
# SUPER
########################################
constraints host: ENV['SUPER_HOST'] do
scope module: :super do
resources :foobars
get '/' => 'super#index'
end
end
Now, move /app/views/foobars to /app/views/super/foobars
and, move /app/controllers/foobars_controller.rb to /app/controllers/super/foobars_controller.rb
Make sure foobars_controller.rb is in the Super module:
class Super::FoobarsController < ApplicationController
Now go to your.dev.server/foobars/
You should get this error:
Routing Error uninitialized constant FoobarsController
Now, remove resources :foobars from beginning of routes.rb
It should work now!
It took me awhile to figure out why I was getting this error, and I didn't realize that generating the scaffold adds an entry in routes.rb
While it doesn't answer your specific question, I received the failure with the following in my routes.rb
resources :republishes do
post '/attempt_all', :to => 'republishes/#attempt_all' . . .
which I changed to
resources :republishes do
post '/attempt_all', :to => 'republishes#attempt_all' . . .
Removing the slash fixed my issue.
In my case, Since I'd scaffold the module, it was already had initiated routes for the controller and I was defining it twice. So by removing one of the duplicate resource routes resolved my issue.
make sure you've created your model for the controller in question.
I'm new with RoR so this is a newbie question:
if I have a controller users_controller.rb and I add a method foo, shouldn't it create this route?
http://www.localhost:3000/users/foo
because when I did that, I got this error:
Couldn't find User with id=foo
I of course added a view foo.html.erb
EDIT:
I added to routes.rb this code but I get the same error:
resources :users do
get "signup"
end
This doesn't work automatically in rails 3. You'll need to add
resource :users do
get "foo"
end
to your routes.rb
You'll definitely want to have a look at http://guides.rubyonrails.org/routing.html, it explains routing pretty well.
Rails is directing you to the show controller and thinks that you're providing foo as :id param to the show action.
You need to set a route that will be dispatched prior to being matched as /users/:id in users#show
You can accomplish this by modifying config/routes.rb by adding the following to replace your existing resource describing :users
resource :users do
get "foo"
end
Just to add to the other answers, in earlier versions of Rails there used to be a default route
match ':controller(/:action(/:id))(.:format)'
which gave the behaviour you describe where a request of the form controller/action would call the given method on the given controller. This line is still in routes.rb but is commented out by default. You can uncomment it to enable this behaviour but the comment above it explains why this is not recommended:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
At the schema ':controller/:action(.:format)', you can also easily do the following
resources :users do
get "foo", on: :collection
end
or
resources :users do
collection do
get 'foo'
end
end
http://guides.rubyonrails.org/routing.html#adding-collection-routes