Related
My setup isn't too complex I think, I have an Event with some RSVPs. My routes file has
map.resources :events, :has_many => :rsvps
map.resources :rsvps, :only => [:new, :create, :destroy, :index], :collection => { :list => :get }
map.resources :events do |event|
event.resources :rsvps, :only => [:new, :create, :destroy, :index], :collection => { :list => :get }
end
Which give me a rake routes
event_rsvps GET /events/:event_id/rsvps(.:format) {:action=>"index", :controller=>"rsvps"}
POST /events/:event_id/rsvps(.:format) {:action=>"create", :controller=>"rsvps"}
new_event_rsvp GET /events/:event_id/rsvps/new(.:format) {:action=>"new", :controller=>"rsvps"}
edit_event_rsvp GET /events/:event_id/rsvps/:id/edit(.:format) {:action=>"edit", :controller=>"rsvps"}
event_rsvp GET /events/:event_id/rsvps/:id(.:format) {:action=>"show", :controller=>"rsvps"}
PUT /events/:event_id/rsvps/:id(.:format) {:action=>"update", :controller=>"rsvps"}
DELETE /events/:event_id/rsvps/:id(.:format) {:action=>"destroy", :controller=>"rsvps"}
events GET /events(.:format) {:action=>"index", :controller=>"events"}
POST /events(.:format) {:action=>"create", :controller=>"events"}
new_event GET /events/new(.:format) {:action=>"new", :controller=>"events"}
edit_event GET /events/:id/edit(.:format) {:action=>"edit", :controller=>"events"}
event GET /events/:id(.:format) {:action=>"show", :controller=>"events"}
PUT /events/:id(.:format) {:action=>"update", :controller=>"events"}
DELETE /events/:id(.:format) {:action=>"destroy", :controller=>"events"}
list_rsvps GET /rsvps/list(.:format) {:action=>"list", :controller=>"rsvps"}
rsvps GET /rsvps(.:format) {:action=>"index", :controller=>"rsvps"}
POST /rsvps(.:format) {:action=>"create", :controller=>"rsvps"}
new_rsvp GET /rsvps/new(.:format) {:action=>"new", :controller=>"rsvps"}
rsvp DELETE /rsvps/:id(.:format) {:action=>"destroy", :controller=>"rsvps"}
list_event_rsvps GET /events/:event_id/rsvps/list(.:format) {:action=>"list", :controller=>"rsvps"}
GET /events/:event_id/rsvps(.:format) {:action=>"index", :controller=>"rsvps"}
POST /events/:event_id/rsvps(.:format) {:action=>"create", :controller=>"rsvps"}
GET /events/:event_id/rsvps/new(.:format) {:action=>"new", :controller=>"rsvps"}
DELETE /events/:event_id/rsvps/:id(.:format) {:action=>"destroy", :controller=>"rsvps"}
GET /events(.:format) {:action=>"index", :controller=>"events"}
POST /events(.:format) {:action=>"create", :controller=>"events"}
GET /events/new(.:format) {:action=>"new", :controller=>"events"}
GET /events/:id/edit(.:format) {:action=>"edit", :controller=>"events"}
GET /events/:id(.:format) {:action=>"show", :controller=>"events"}
PUT /events/:id(.:format) {:action=>"update", :controller=>"events"}
DELETE /events/:id(.:format) {:action=>"destroy", :controller=>"events"}
I then have a
<%= link_to 'List Attending', list_event_rsvps_path(event) %>
But when I click this view I get
Unknown action
No action responded to show. Actions: create, current_admin,
current_admin=, destroy, index, list, new, sign_in, sign_out, and
signed_in?
the server log shows
Processing RsvpsController#show (for 127.0.0.1 at 2014-01-10 16:12:18)
[GET] Parameters: {"id"=>"list", "event_id"=>"1"}
ActionController::UnknownAction (No action responded to show. Actions:
create, current_admin, current_admin=, destroy, index, list, new,
sign_in, sign_out, and signed_in?):
c:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:104:in service'
c:/Ruby187/lib/ruby/1.8/webrick/httpserver.rb:65:inrun'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:173:in start_thread'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:instart'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:162:in start_thread'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:95:instart'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:in each'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:92:instart'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:23:in start'
c:/Ruby187/lib/ruby/1.8/webrick/server.rb:82:instart'
At this point I've tried everything I could find about the subject. I'm using ruby 1.8.7 and Rail 2.3.18 since my web host is using that still. I have no clue why it's still looking for the show action when I've removed it from the controller and all the routes.
The routes are applied in the order they are defined. Try putting:
map.resources :events, :has_many => :rsvps
Below the other routes.
I have a small web app in Ruby on Rails which lists antique documents and the locations that are referenced by the documents. I can update locations that are already attached to documents, but when trying to create a new document, I get the following error
undefined method `locations' for nil:NilClass
The NilClass occurs in LocationController#new:
def new
#location = #document.locations.new
end
This is called from the view as such
<%= form_for(#location,:url=>new_document_location_path(#document)) do |f| %>
<%= render :partial => "document_locations/form", :locals =>{:f=>f} %>
<%= f.submit "Save",:class=>"span2 btn" %>
<%end%>
So for some reason, the #document class isn't being referenced by the controller. I know the class is accessible to the view and in scope as I am able to view document attributes within the form (eg <%= #document.id %>
So, why is the Controller not able to reference the #document variable? I'm assuming this is down to how I am passing the class variable. Eitherway, I'd be very grateful for some pointers.
This is how the models are defined
class Document < ActiveRecord::Base
has_many :locations, :dependent => :destroy
end
class Location < ActiveRecord::Base
belongs_to :document
end
Here's my routes
Pastpaper::Application.routes.draw do
# added in to test
match '/documents/:id/locations/create' => 'locations#create'
resources :documents do
collection do
match 'permanent_delete/:id' => 'documents#permanently_delete',:as => :permanent_delete
end
match '/authorinfo' => 'documents#authorinfo',:as => :authorinfo
match '/publicationinfo' => 'documents#publicationinfo',:as => :publicationinfo
match '/images' => 'documents#itemimages' ,:as => :itemimages
match '/locations' => 'documents#locations',:as => :locations
match '/itempeople' => 'documents#itempeople' ,:as => :itempeople
match '/people_facts_locations' => 'documents#people_facts_locations',:as => :people_facts_loc
resources :locations, :controller=>"locations"
resources :people ,:controller => "document_people"
resources :document_facts
resource :facts
resources :document_photos
end
resources :home do
collection do
get 'document_search','simple_location_search','date_search','simple_people_search','simple_organisation_search','document_filter'
get 'search_results'
post 'search_results'
end
end
match 'documents/publicationinfo/:id' => 'documents#publicationinfo',:as => :publicationinfo
match 'documents/update/publishinginfo/:id' => 'documents#publishinginfo',:as => :publishinginfo
match 'documents/document_image_remove/:id' => 'documents#remove_image',:as=>"remove_image"
match 'documents/make_primary_image/:id' => 'documents#make_primary_image',:as => :make_primary_image
match 'person_detail/:id' => 'documents#person_detail',:as=>'person_detail'
match 'about', :to=> 'pages#about'
match 'contact', :to => 'pages#contact'
match 'privacy', :to => 'pages#privacy'
match 'terms', :to => 'pages#terms'
match 'help', :to => 'pages#help'
namespace :admin do
resources :document_types
resources :statuses
resources :attribute_types
resources :event_types
resources :users
resources :orders
resources :documents
match 'restore_document/:id' => 'Documents#restore_document', :as => 'restore_document'
match 'report' => 'report#index' ,:as=>:report
match 'report/surname_report' => 'report#surname_report',:as=>:surname_report
match 'report/location_report' => 'report#location_report',:as=>:location_report
end
root :to => 'home#index'
end
And here's the relevant Controllers:
DocumentController
class DocumentsController < ApplicationController
before_filter :prepare_document ,:only => [:locations]
def locations
#locations = #document.locations.order("id asc")
#location = #document.locations.new
end
private
def prepare_document
if params[:id]
#document = Document.find(params[:id], :include => [:document_attributes])
elsif params[:document_id]
#document = Document.find(params[:document_id], :include => [:document_attributes])
end
end
end
LocationsController
class LocationsController < ApplicationController
before_filter :prepare_document
def new
#location = #document.locations.new
end
def create
#location = #document.locations.build
if #location.save
redirect_to document_locations_url(#document)
else
render "new"
end
end
def update
#location = #document.locations.find(params[:id])
if #location.update_attributes(params[:location])
redirect_to document_locations_url(#document)
else
render "edit"
end
end
def prepare_document
if params[:document_id]
if current_user.is_admin?
#document = Document.find(params[:document_id], :include => [:document_attributes])
else
#document = current_user.documents.find(params[:document_id], :include => [:document_attributes])
end
end
end
end
This is how my routes rake out
logout GET /logout(.:format) {:action=>"destroy", :controller=>"sessions"}
login POST /login(.:format) {:action=>"create", :controller=>"sessions"}
forgot_password /forgot_password(.:format) {:action=>"new", :controller=>"password_resets"}
user_home GET /profile(.:format) {:action=>"index", :controller=>"users"}
deactivateaccount /account/deactivate(.:format) {:controller=>"users", :action=>"accountdeactivate"}
changepassword /account/changepassword(.:format) {:controller=>"users", :action=>"changepassword"}
register /user/registration(.:format) {:controller=>"users", :action=>"new"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
password_resets GET /password_resets(.:format) {:action=>"index", :controller=>"password_resets"}
POST /password_resets(.:format) {:action=>"create", :controller=>"password_resets"}
new_password_reset GET /password_resets/new(.:format) {:action=>"new", :controller=>"password_resets"}
edit_password_reset GET /password_resets/:id/edit(.:format) {:action=>"edit", :controller=>"password_resets"}
password_reset GET /password_resets/:id(.:format) {:action=>"show", :controller=>"password_resets"}
PUT /password_resets/:id(.:format) {:action=>"update", :controller=>"password_resets"}
DELETE /password_resets/:id(.:format) {:action=>"destroy", :controller=>"password_resets"}
password_reset_path /password_resets/:id/edit(.:format) {:controller=>"password_resets", :action=>"edit"}
/documents/:id/locations/create(.:format) {:controller=>"locations", :action=>"create"}
permanent_delete_documents /documents/permanent_delete/:id(.:format) {:controller=>"documents", :action=>"permanently_delete"}
document_authorinfo /documents/:document_id/authorinfo(.:format) {:controller=>"documents", :action=>"authorinfo"}
document_publicationinfo /documents/:document_id/publicationinfo(.:format) {:controller=>"documents", :action=>"publicationinfo"}
document_itemimages /documents/:document_id/images(.:format) {:controller=>"documents", :action=>"itemimages"}
document_locations /documents/:document_id/locations(.:format) {:controller=>"documents", :action=>"locations"}
document_itempeople /documents/:document_id/itempeople(.:format) {:controller=>"documents", :action=>"itempeople"}
document_people_facts_loc /documents/:document_id/people_facts_locations(.:format) {:controller=>"documents", :action=>"people_facts_locations"}
GET /documents/:document_id/locations(.:format) {:action=>"index", :controller=>"locations"}
POST /documents/:document_id/locations(.:format) {:action=>"create", :controller=>"locations"}
new_document_location GET /documents/:document_id/locations/new(.:format) {:action=>"new", :controller=>"locations"}
edit_document_location GET /documents/:document_id/locations/:id/edit(.:format) {:action=>"edit", :controller=>"locations"}
document_location GET /documents/:document_id/locations/:id(.:format) {:action=>"show", :controller=>"locations"}
PUT /documents/:document_id/locations/:id(.:format) {:action=>"update", :controller=>"locations"}
DELETE /documents/:document_id/locations/:id(.:format) {:action=>"destroy", :controller=>"locations"}
document_people GET /documents/:document_id/people(.:format) {:action=>"index", :controller=>"document_people"}
POST /documents/:document_id/people(.:format) {:action=>"create", :controller=>"document_people"}
new_document_person GET /documents/:document_id/people/new(.:format) {:action=>"new", :controller=>"document_people"}
edit_document_person GET /documents/:document_id/people/:id/edit(.:format) {:action=>"edit", :controller=>"document_people"}
document_person GET /documents/:document_id/people/:id(.:format) {:action=>"show", :controller=>"document_people"}
PUT /documents/:document_id/people/:id(.:format) {:action=>"update", :controller=>"document_people"}
DELETE /documents/:document_id/people/:id(.:format) {:action=>"destroy", :controller=>"document_people"}
document_document_facts GET /documents/:document_id/document_facts(.:format) {:action=>"index", :controller=>"document_facts"}
POST /documents/:document_id/document_facts(.:format) {:action=>"create", :controller=>"document_facts"}
new_document_document_fact GET /documents/:document_id/document_facts/new(.:format) {:action=>"new", :controller=>"document_facts"}
edit_document_document_fact GET /documents/:document_id/document_facts/:id/edit(.:format) {:action=>"edit", :controller=>"document_facts"}
document_document_fact GET /documents/:document_id/document_facts/:id(.:format) {:action=>"show", :controller=>"document_facts"}
PUT /documents/:document_id/document_facts/:id(.:format) {:action=>"update", :controller=>"document_facts"}
DELETE /documents/:document_id/document_facts/:id(.:format) {:action=>"destroy", :controller=>"document_facts"}
document_facts POST /documents/:document_id/facts(.:format) {:action=>"create", :controller=>"facts"}
new_document_facts GET /documents/:document_id/facts/new(.:format) {:action=>"new", :controller=>"facts"}
edit_document_facts GET /documents/:document_id/facts/edit(.:format) {:action=>"edit", :controller=>"facts"}
GET /documents/:document_id/facts(.:format) {:action=>"show", :controller=>"facts"}
PUT /documents/:document_id/facts(.:format) {:action=>"update", :controller=>"facts"}
DELETE /documents/:document_id/facts(.:format) {:action=>"destroy", :controller=>"facts"}
document_document_photos GET /documents/:document_id/document_photos(.:format) {:action=>"index", :controller=>"document_photos"}
POST /documents/:document_id/document_photos(.:format) {:action=>"create", :controller=>"document_photos"}
new_document_document_photo GET /documents/:document_id/document_photos/new(.:format) {:action=>"new", :controller=>"document_photos"}
edit_document_document_photo GET /documents/:document_id/document_photos/:id/edit(.:format) {:action=>"edit", :controller=>"document_photos"}
document_document_photo GET /documents/:document_id/document_photos/:id(.:format) {:action=>"show", :controller=>"document_photos"}
PUT /documents/:document_id/document_photos/:id(.:format) {:action=>"update", :controller=>"document_photos"}
DELETE /documents/:document_id/document_photos/:id(.:format) {:action=>"destroy", :controller=>"document_photos"}
documents GET /documents(.:format) {:action=>"index", :controller=>"documents"}
POST /documents(.:format) {:action=>"create", :controller=>"documents"}
new_document GET /documents/new(.:format) {:action=>"new", :controller=>"documents"}
edit_document GET /documents/:id/edit(.:format) {:action=>"edit", :controller=>"documents"}
document GET /documents/:id(.:format) {:action=>"show", :controller=>"documents"}
PUT /documents/:id(.:format) {:action=>"update", :controller=>"documents"}
DELETE /documents/:id(.:format) {:action=>"destroy", :controller=>"documents"}
paypal_cancel /payments/cancel(.:format) {:controller=>"payments", :action=>"paypal_cancel"}
paypal_return /payments/success(.:format) {:controller=>"payments", :action=>"paypal_return"}
paypal_ipn /payments/ipn(.:format) {:controller=>"payments", :action=>"create"}
document_search_home_index GET /home/document_search(.:format) {:action=>"document_search", :controller=>"home"}
simple_location_search_home_index GET /home/simple_location_search(.:format) {:action=>"simple_location_search", :controller=>"home"}
date_search_home_index GET /home/date_search(.:format) {:action=>"date_search", :controller=>"home"}
simple_people_search_home_index GET /home/simple_people_search(.:format) {:action=>"simple_people_search", :controller=>"home"}
simple_organisation_search_home_index GET /home/simple_organisation_search(.:format) {:action=>"simple_organisation_search", :controller=>"home"}
document_filter_home_index GET /home/document_filter(.:format) {:action=>"document_filter", :controller=>"home"}
search_results_home_index GET /home/search_results(.:format) {:action=>"search_results", :controller=>"home"}
POST /home/search_results(.:format) {:action=>"search_results", :controller=>"home"}
home_index GET /home(.:format) {:action=>"index", :controller=>"home"}
POST /home(.:format) {:action=>"create", :controller=>"home"}
new_home GET /home/new(.:format) {:action=>"new", :controller=>"home"}
edit_home GET /home/:id/edit(.:format) {:action=>"edit", :controller=>"home"}
home GET /home/:id(.:format) {:action=>"show", :controller=>"home"}
PUT /home/:id(.:format) {:action=>"update", :controller=>"home"}
DELETE /home/:id(.:format) {:action=>"destroy", :controller=>"home"}
publicationinfo /documents/publicationinfo/:id(.:format) {:controller=>"documents", :action=>"publicationinfo"}
publishinginfo /documents/update/publishinginfo/:id(.:format) {:controller=>"documents", :action=>"publishinginfo"}
remove_image /documents/document_image_remove/:id(.:format) {:controller=>"documents", :action=>"remove_image"}
make_primary_image /documents/make_primary_image/:id(.:format) {:controller=>"documents", :action=>"make_primary_image"}
person_detail /person_detail/:id(.:format) {:controller=>"documents", :action=>"person_detail"}
about /about(.:format) {:action=>"about", :controller=>"pages"}
contact /contact(.:format) {:action=>"contact", :controller=>"pages"}
privacy /privacy(.:format) {:action=>"privacy", :controller=>"pages"}
terms /terms(.:format) {:action=>"terms", :controller=>"pages"}
help /help(.:format) {:action=>"help", :controller=>"pages"}
def new
#location = #document.locations.build
end
Use build not new. And make your prepare_document private in your controller.
Also
<%= form_for([#document, #location]) do |f| %>
Your url parameter was wrong, you shouldn't post forms to the new action, you should post them to the update action like I showed above.
I have an Enki blog set up and the sign in's at www.localhost:3000/admin.`
I'm guessing that this admin url is determined by this code from routes, but, for extra security reasons, I'd like to make the login, for example,
www.localhost:3000/ilovejesus
Enki::Application.routes.draw do
namespace 'admin' do
resource :session
Based on what rake routes is telling me, I'm guessing it's the admin_root that goes to :controller => Admin/Dashboard, :action => 'show' that needs to get changed.
So can anyone tell me how I might change this too, for example, localhost:3000/iloveronpaul
Rake Routes:
admin_session POST /admin/session(.:format) {:action=>"create", :controller=>"admin/sessions"}
new_admin_session GET /admin/session/new(.:format) {:action=>"new", :controller=>"admin/sessions"}
edit_admin_session GET /admin/session/edit(.:format) {:action=>"edit", :controller=>"admin/sessions"}
GET /admin/session(.:format) {:action=>"show", :controller=>"admin/sessions"}
PUT /admin/session(.:format) {:action=>"update", :controller=>"admin/sessions"}
DELETE /admin/session(.:format) {:action=>"destroy", :controller=>"admin/sessions"}
preview_admin_posts POST /admin/posts/preview(.:format) {:action=>"preview", :controller=>"admin/posts"}
admin_posts GET /admin/posts(.:format) {:action=>"index", :controller=>"admin/posts"}
POST /admin/posts(.:format) {:action=>"create", :controller=>"admin/posts"}
new_admin_post GET /admin/posts/new(.:format) {:action=>"new", :controller=>"admin/posts"}
edit_admin_post GET /admin/posts/:id/edit(.:format) {:action=>"edit", :controller=>"admin/posts"}
admin_post GET /admin/posts/:id(.:format) {:action=>"show", :controller=>"admin/posts"}
PUT /admin/posts/:id(.:format) {:action=>"update", :controller=>"admin/posts"}
DELETE /admin/posts/:id(.:format) {:action=>"destroy", :controller=>"admin/posts"}
preview_admin_pages POST /admin/pages/preview(.:format) {:action=>"preview", :controller=>"admin/pages"}
admin_pages GET /admin/pages(.:format) {:action=>"index", :controller=>"admin/pages"}
POST /admin/pages(.:format) {:action=>"create", :controller=>"admin/pages"}
new_admin_page GET /admin/pages/new(.:format) {:action=>"new", :controller=>"admin/pages"}
edit_admin_page GET /admin/pages/:id/edit(.:format) {:action=>"edit", :controller=>"admin/pages"}
admin_page GET /admin/pages/:id(.:format) {:action=>"show", :controller=>"admin/pages"}
PUT /admin/pages/:id(.:format) {:action=>"update", :controller=>"admin/pages"}
DELETE /admin/pages/:id(.:format) {:action=>"destroy", :controller=>"admin/pages"}
admin_comments GET /admin/comments(.:format) {:action=>"index", :controller=>"admin/comments"}
POST /admin/comments(.:format) {:action=>"create", :controller=>"admin/comments"}
new_admin_comment GET /admin/comments/new(.:format) {:action=>"new", :controller=>"admin/comments"}
edit_admin_comment GET /admin/comments/:id/edit(.:format) {:action=>"edit", :controller=>"admin/comments"}
admin_comment GET /admin/comments/:id(.:format) {:action=>"show", :controller=>"admin/comments"}
PUT /admin/comments/:id(.:format) {:action=>"update", :controller=>"admin/comments"}
DELETE /admin/comments/:id(.:format) {:action=>"destroy", :controller=>"admin/comments"}
undo_admin_undo_item POST /admin/undo_items/:id/undo(.:format) {:action=>"undo", :controller=>"admin/undo_items"}
admin_undo_items GET /admin/undo_items(.:format) {:action=>"index", :controller=>"admin/undo_items"}
POST /admin/undo_items(.:format) {:action=>"create", :controller=>"admin/undo_items"}
new_admin_undo_item GET /admin/undo_items/new(.:format) {:action=>"new", :controller=>"admin/undo_items"}
edit_admin_undo_item GET /admin/undo_items/:id/edit(.:format) {:action=>"edit", :controller=>"admin/undo_items"}
admin_undo_item GET /admin/undo_items/:id(.:format) {:action=>"show", :controller=>"admin/undo_items"}
PUT /admin/undo_items/:id(.:format) {:action=>"update", :controller=>"admin/undo_items"}
DELETE /admin/undo_items/:id(.:format) {:action=>"destroy", :controller=>"admin/undo_items"}
admin_health /admin/health(/:action)(.:format) {:action=>"index", :controller=>"admin/health"}
admin_root /admin(.:format) {:controller=>"admin/dashboard", :action=>"show"}
archives GET /archives(.:format) {:action=>"index", :controller=>"archives"}
page GET /pages/:id(.:format) {:action=>"show", :controller=>"pages"}
GET /:year/:month/:day/:slug/comments(.:format) {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"comments", :action=>"index"}
POST /:year/:month/:day/:slug/comments(.:format) {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"comments", :action=>"create"}
GET /:year/:month/:day/:slug/comments/new(.:format) {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"comments", :action=>"new"}
GET /:year/:month/:day/:slug(.:format) {:year=>/\d{4}/, :month=>/\d{2}/, :day=>/\d{2}/, :controller=>"posts", :action=>"show"}
formatted_posts GET /posts.:format {:controller=>"posts", :action=>"index"}
posts GET /(:tag)(.:format) {:controller=>"posts", :action=>"index"}
root /(.:format) {:controller=>"posts", :action=>"index"}
Add a :path option to your namespace
Enki::Application.routes.draw do
scope :module => 'admin' do
resource :session, :path_names => { :new => "liverandonions",
:show => "ilovejesus,
:edit => "iloveronpaul" }
I'm not sure but the path_names options might have to be wrapped in array.
ie.
resource :session, :path_names => { [:new => "liverandonions",
:show => "ilovejesus,
:edit => "iloveronpaul"] }
let me know which one works.
I am in the process updating my app so I use the Devise gem for authentication. Everything appears to be working great, except for the fact that I can't seem to sign out.
I get the error:
Couldn't find User with ID=sign_out
Parameters:
{"id"=>"sign_out"}
I can trace the error back to the show action in my users controller:
def show
#user = User.find(params[:id])
end
The problem is that I am not sure why it is trying to render the show action for my user. Overall my page has this format:
<% if user_signed_in? %>
<%= render 'shared/feed_home' %>
<% else %>
<%= render 'shared/splash' %>
<% end %>
As per devise instructions, my sign-out path looks like this:
<li><%= link_to "Sign out", destroy_user_session_path %></li>
If a user is not signed in, it should render the splash page which is basically static html. Any suggestions on how to help? Even if you could just put me in the right ball park in terms of the problem that would be much appreciated.
Here is my routes file:
devise_for :users
resources :users do
member do
get :following, :followers, :following_tags, :following_posts
end
end
resources :posts
resources :votes
resources :comments
resources :tags
resources :events
#resources :posts, :only => [:create, :destroy, :show]
resources :relationships, :only => [:create, :destroy]
root :to =>'pages#subscribed'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/live', :to => "pages#home"
match '/voted', :to => 'pages#highest_voted'
match '/signup', :to => 'users#new'
Here is my rake routes:
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
following_user GET /users/:id/following(.:format) {:action=>"following", :controller=>"users"}
followers_user GET /users/:id/followers(.:format) {:action=>"followers", :controller=>"users"}
following_tags_user GET /users/:id/following_tags(.:format) {:action=>"following_tags", :controller=>"users"}
following_posts_user GET /users/:id/following_posts(.:format) {:action=>"following_posts", :controller=>"users"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
POST /posts(.:format) {:action=>"create", :controller=>"posts"}
new_post GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
votes GET /votes(.:format) {:action=>"index", :controller=>"votes"}
POST /votes(.:format) {:action=>"create", :controller=>"votes"}
new_vote GET /votes/new(.:format) {:action=>"new", :controller=>"votes"}
edit_vote GET /votes/:id/edit(.:format) {:action=>"edit", :controller=>"votes"}
vote GET /votes/:id(.:format) {:action=>"show", :controller=>"votes"}
PUT /votes/:id(.:format) {:action=>"update", :controller=>"votes"}
DELETE /votes/:id(.:format) {:action=>"destroy", :controller=>"votes"}
comments GET /comments(.:format) {:action=>"index", :controller=>"comments"}
POST /comments(.:format) {:action=>"create", :controller=>"comments"}
new_comment GET /comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_comment GET /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
tags GET /tags(.:format) {:action=>"index", :controller=>"tags"}
POST /tags(.:format) {:action=>"create", :controller=>"tags"}
new_tag GET /tags/new(.:format) {:action=>"new", :controller=>"tags"}
edit_tag GET /tags/:id/edit(.:format) {:action=>"edit", :controller=>"tags"}
tag GET /tags/:id(.:format) {:action=>"show", :controller=>"tags"}
PUT /tags/:id(.:format) {:action=>"update", :controller=>"tags"}
DELETE /tags/:id(.:format) {:action=>"destroy", :controller=>"tags"}
events GET /events(.:format) {:action=>"index", :controller=>"events"}
POST /events(.:format) {:action=>"create", :controller=>"events"}
new_event GET /events/new(.:format) {:action=>"new", :controller=>"events"}
edit_event GET /events/:id/edit(.:format) {:action=>"edit", :controller=>"events"}
event GET /events/:id(.:format) {:action=>"show", :controller=>"events"}
PUT /events/:id(.:format) {:action=>"update", :controller=>"events"}
DELETE /events/:id(.:format) {:action=>"destroy", :controller=>"events"}
relationships POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
relationship DELETE /relationships/:id(.:format) {:action=>"destroy", :controller=>"relationships"}
root /(.:format) {:controller=>"pages", :action=>"subscribed"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
live /live(.:format) {:controller=>"pages", :action=>"home"}
voted /voted(.:format) {:controller=>"pages", :action=>"highest_voted"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
Actually, ignore my previous answer and try this:
<li><%= link_to "Sign out", destroy_user_session_path, :method => :delete %></li>
The problem might be in your routes.rb file. You need to route user/signout appropriately and it needs to be above the route for your users, because routes work from the top down. If you post your routes file, I can help more.
Do you maybe have a resources :users above your devise_for :users?
I guess you can also try scoping the devise routes:
devise_scope :users do
get "sign_out", :to => "devise/sessions#destroy"
end
I have the followed Rails 3 routes:
Hello::Application.routes.draw do
resources :blogs do
resources :articles do
resources :comments
end
end
end
By raking them, we can find:
GET /blogs/:blog_id/articles/:article_id/comments(.:format) {:action=>"index", :controller=>"comments"}
blog_article_comments POST /blogs/:blog_id/articles/:article_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_blog_article_comment GET /blogs/:blog_id/articles/:article_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
GET /blogs/:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /blogs/:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
blog_article_comment DELETE /blogs/:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
edit_blog_article_comment GET /blogs/:blog_id/articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
GET /blogs/:blog_id/articles(.:format) {:action=>"index", :controller=>"articles"}
blog_articles POST /blogs/:blog_id/articles(.:format) {:action=>"create", :controller=>"articles"}
new_blog_article GET /blogs/:blog_id/articles/new(.:format) {:action=>"new", :controller=>"articles"}
GET /blogs/:blog_id/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /blogs/:blog_id/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
blog_article DELETE /blogs/:blog_id/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
edit_blog_article GET /blogs/:blog_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
blogs POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
blog DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
Because every routes begin with the same root path (/blogs), I would like to shorten addresses by removing it (when :blog_id is given).
In this why, I could have thoses routes (I think it's more DRY):
GET /:blog_id/articles/:article_id/comments(.:format) {:action=>"index", :controller=>"comments"}
blog_article_comments POST /:blog_id/articles/:article_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_blog_article_comment GET /:blog_id/articles/:article_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
GET /:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
blog_article_comment DELETE /:blog_id/articles/:article_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
edit_blog_article_comment GET /:blog_id/articles/:article_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
GET /:blog_id/articles(.:format) {:action=>"index", :controller=>"articles"}
blog_articles POST /:blog_id/articles(.:format) {:action=>"create", :controller=>"articles"}
new_blog_article GET /:blog_id/articles/new(.:format) {:action=>"new", :controller=>"articles"}
GET /:blog_id/articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /:blog_id/articles/:id(.:format) {:action=>"update", :controller=>"articles"}
blog_article DELETE /:blog_id/articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
edit_blog_article GET /:blog_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
blogs POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
blog DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
According to you, what kind of change I should make over my current routes configuration?
Thanks.
My opinion is that you need to look at shallow routes. To my knowledge the customisation you are asking is not achievable using route resources and I don't think it's desirable.
Where you have defined one to many relationships you do not need to pass the identifier for each of the nested resources. This is considered bad practice (by some, though not others). For example, instead of this long url:
/blog/:blog_id/articles/:article_id/comments/:id
#controller
#blog = Blog.find params[:blog_id]
#article = Blog.find params[:article_id]
#comment = Comment.find params[:id]
you actually need only:
/comments/:id
#controller
#comment = Comment.find params[:id]
#article = #comment.article
#blog = #article.blog
More info:
http://railscasts.com/episodes/139-nested-resources
"Resources should never be nested more than 1 level deep." (http://guides.rubyonrails.org/routing.html)
If the Rails documentation is making this strong of a recommendation I have no idea why it is allowed still.