Ruby on Rails Class Variable not being referenced by controller - ruby-on-rails

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.

Related

I keep getting the show action instead of a custom action when using a nest route

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.

Rspec namespaced route specs failing even though route exists

I'm using rspec-rails (2.8.1) and rails 3.1.3.
I'm trying to test routes for my Admin::ZonesController. I have verified the route exists in both the browser and by running rake routes. I am not using ActiveRecord (if that matters). When I run the routing spec, it tells me:
ActionController::RoutingError: No route matches "/admin/zones/new"
Here is the test (spec/routing/admin/zones_routing_spec.rb):
require 'spec_helper'
describe "routing to zones" do
it "routes /admin/zones/new to admin/zones#new" do
{ :get => "/admin/zones/new" }.should route_to(
:controller => "admin/zones",
:action => "new"
)
end
end
Here is the controller action whose route I am trying to test (admin/zones#new):
class Admin::ZonesController < Admin::BaseController
before_filter :instantiate_variables
def new
#zone = Zone.new
#campaign = Campaign.new
#rules = [Rule.new]
end
end
Running rake routes gives me this:
POST /hooks/:resource(.:format) {:controller=>"hooks", :action=>"create"}
POST /services/:service/:method(.:format) {:controller=>"services", :action=>"create"}
admin_zones GET /admin/zones(.:format) {:action=>"index", :controller=>"admin/zones"}
POST /admin/zones(.:format) {:action=>"create", :controller=>"admin/zones"}
new_admin_zone GET /admin/zones/new(.:format) {:action=>"new", :controller=>"admin/zones"}
edit_admin_zone GET /admin/zones/:id/edit(.:format) {:action=>"edit", :controller=>"admin/zones"}
admin_zone GET /admin/zones/:id(.:format) {:action=>"show", :controller=>"admin/zones"}
PUT /admin/zones/:id(.:format) {:action=>"update", :controller=>"admin/zones"}
DELETE /admin/zones/:id(.:format) {:action=>"destroy", :controller=>"admin/zones"}
admin_widgets GET /admin/widgets(.:format) {:action=>"index", :controller=>"admin/widgets"}
POST /admin/widgets(.:format) {:action=>"create", :controller=>"admin/widgets"}
new_admin_widget GET /admin/widgets/new(.:format) {:action=>"new", :controller=>"admin/widgets"}
edit_admin_widget GET /admin/widgets/:id/edit(.:format) {:action=>"edit", :controller=>"admin/widgets"}
admin_widget GET /admin/widgets/:id(.:format) {:action=>"show", :controller=>"admin/widgets"}
PUT /admin/widgets/:id(.:format) {:action=>"update", :controller=>"admin/widgets"}
DELETE /admin/widgets/:id(.:format) {:action=>"destroy", :controller=>"admin/widgets"}
zones GET /zones(.:format) {:action=>"index", :controller=>"zones"}
POST /zones(.:format) {:action=>"create", :controller=>"zones"}
new_zone GET /zones/new(.:format) {:action=>"new", :controller=>"zones"}
edit_zone GET /zones/:id/edit(.:format) {:action=>"edit", :controller=>"zones"}
zone GET /zones/:id(.:format) {:action=>"show", :controller=>"zones"}
PUT /zones/:id(.:format) {:action=>"update", :controller=>"zones"}
DELETE /zones/:id(.:format) {:action=>"destroy", :controller=>"zones"}
root / {:controller=>"admin/zones", :action=>"new"}
My routes.rb looks like this:
D2CModularPlatform::Application.routes.draw do
post "/hooks/:resource" => "hooks#create"
post "/services/:service/:method" => "services#create"
namespace :admin do
resources :zones
resources :widgets
end
resources :zones
root :to => "admin/zones#new"
end
My controllers dir looks like this:
controllers
admin
base_controller
widgets_controller
zones_controller
application_controller
hooks_controller
services_controller
zones_controller
My spec/routing dir looks like this:
spec/routing
admin
zones_routing_spec
hooks_routing_spec
services_routing_spec
zones_routing_spec

Problem Signing Out with Devise on my App

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

Rails & Devise: Two-step confirmation route error

I'm trying to make a two-step confirmation like heroku using Devise.
My routes:
devise_for :user, :controllers => {:confirmations => "confirmations", :registrations => "registrations" }
put "confirm_account", :to => "confirmations#confirm_account"
Here's my alternate confirmation controller:
class ConfirmationsController < Devise::ConfirmationsController
def show
#account = User.find_by_confirmation_token(params[:confirmation_token])
if !#account.present?
render_with_scope :new
end
end
def confirm_account
#account = User.find(params[:account][:confirmation_token])
if #account.update_attributes(params[:account]) and #account.password_match?
#account = User.confirm_by_token(#account.confirmation_token)
set_flash_message :notice, :confirmed
sign_in_and_redirect("user", #account)
else
render :action => "show"
end
end
end
Here's my show.html.erb:
<%= form_for(resource, :as => resource_name, :url => confirm_account_path(resource_name)) do |f| %>
<%= f.label :email %>
<%= #account.email %>
<%= f.hidden_field :confirmation_token %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit 'Confirm Account' %>
<%= link_to 'Home', root_url %>
<%= render :partial => 'devise/shared/links' %>
<% end %>
When I click confirm after filling out the password (after clicking confirm in the confirmation email). I'm routed to /confirm_account.user That's pretty weird, right? What's going on to cause this problem?
Edit
rake routes returns:
new_user_session GET /user/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /user/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /user/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /user/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /user/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /user/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /user/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /user/cancel(.:format) {:action=>"cancel", :controller=>"registrations"}
user_registration POST /user(.:format) {:action=>"create", :controller=>"registrations"}
new_user_registration GET /user/sign_up(.:format) {:action=>"new", :controller=>"registrations"}
edit_user_registration GET /user/edit(.:format) {:action=>"edit", :controller=>"registrations"}
PUT /user(.:format) {:action=>"update", :controller=>"registrations"}
DELETE /user(.:format) {:action=>"destroy", :controller=>"registrations"}
user_confirmation POST /user/confirmation(.:format) {:action=>"create", :controller=>"confirmations"}
new_user_confirmation GET /user/confirmation/new(.:format) {:action=>"new", :controller=>"confirmations"}
GET /user/confirmation(.:format) {:action=>"show", :controller=>"confirmations"}
user_unlock POST /user/unlock(.:format) {:action=>"create", :controller=>"devise/unlocks"}
new_user_unlock GET /user/unlock/new(.:format) {:action=>"new", :controller=>"devise/unlocks"}
GET /user/unlock(.:format) {:action=>"show", :controller=>"devise/unlocks"}
confirm_account PUT /confirm_account(.:format) {:action=>"confirm_account", :controller=>"confirmations"}
editreject_admin GET /admin/:id/editreject(.:format) {:action=>"editreject", :controller=>"admin"}
reject_admin GET /admin/:id/reject(.:format) {:action=>"reject", :controller=>"admin"}
accept_admin GET /admin/:id/accept(.:format) {:action=>"accept", :controller=>"admin"}
entries_admin_index GET /admin/entries(.:format) {:action=>"entries", :controller=>"admin"}
preferences_admin_index GET /admin/preferences(.:format) {:action=>"preferences", :controller=>"admin"}
admin_index GET /admin(.:format) {:action=>"index", :controller=>"admin"}
about_entries GET /entries/about(.:format) {:action=>"about", :controller=>"entries"}
all_entries GET /entries/all(.:format) {:action=>"all", :controller=>"entries"}
myentries_entries GET /entries/myentries(.:format) {:action=>"myentries", :controller=>"entries"}
rate_entry GET /entries/:id/rate(.:format) {:action=>"rate", :controller=>"entries"}
submit_entry PUT /entries/:id/submit(.:format) {:action=>"submit", :controller=>"entries"}
entry_comments POST /entries/:entry_id/comments(.:format) {:action=>"create", :controller=>"comments"}
entry_comment DELETE /entries/:entry_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
entries GET /entries(.:format) {:action=>"index", :controller=>"entries"}
POST /entries(.:format) {:action=>"create", :controller=>"entries"}
new_entry GET /entries/new(.:format) {:action=>"new", :controller=>"entries"}
edit_entry GET /entries/:id/edit(.:format) {:action=>"edit", :controller=>"entries"}
entry GET /entries/:id(.:format) {:action=>"show", :controller=>"entries"}
PUT /entries/:id(.:format) {:action=>"update", :controller=>"entries"}
DELETE /entries/:id(.:format) {:action=>"destroy", :controller=>"entries"}
/auth/:service/callback(.:format) {:controller=>"services", :action=>"create"}
services GET /services(.:format) {:action=>"index", :controller=>"services"}
POST /services(.:format) {:action=>"create", :controller=>"services"}
root /(.:format) {:controller=>"entries", :action=>"index"}
offline /offline(.:format) {:controller=>"application", :action=>"offline"}
Edit 3
In changing
devise_for :user, :controllers => {:confirmations => "confirmations", :registrations => "registrations" } do
match "/confirm_account", :to => "confirmations#confirm_account"
end
I'm receiving :
You have a nil object when you didn't
expect it! You might have expected an
instance of Array. The error occurred
while evaluating nil.[]
{"utf8"=>"✓",
"authenticity_token"=>"dsG/e8Tw2Oi6zEDb07R/L0yDOKFEFlse+IgLbfz3Lo0=",
"user"=>{"confirmation_token"=>"",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Confirm Account"}
There's definitely a token in the url, though...This is actually going somewhere, though!
It looks to me like confirm_account_path doesn't exist?
If you didn't set up your routes manually, you can go ahead and set that in the config/routes.rb file, to confirmations#confirm_account.
Or, if you set Devise to use your ConfirmationsController, using new_user_confirmation_path may work too (and may not). Type rake routes in the console to see available routes. They should lead to the ConfirmationsController and the confirm_account action.
EDIT: Try editing your routes file as follows.
devise_for :user, :controllers => {:confirmations => "confirmations", :registrations => "registrations" } do
match "/confirm_account" => "confirmations#confirm_account"
end
I think the slash is important before confirm_account because it is now inside the devise_for block (which is identical to devise_scope). Otherwise it may go to users/confirm_account.
EDIT2: Use params[:user][:confirmation_token], not params[:account][:confirmation_token] in the controller. But currently it looks like the confirmation token is blank.

Ruby on Rails Show Nested ActiveResources

I have an app which connects to a REST rails app. I have two resources: category and post; post is nested in category.
I was able to successfully CRUD categories. I am only able to list posts but no success in showing or updating.
Here is my sample code:
config/routes.rb:
resources :categories do
resources :posts
end
resources :posts do
resources :comments
end
resources :comments
models/post.rb:
class Post < Connector
self.site += "/categories/:category_id"
end
controllers/posts_controller.rb:
class PostsController < ApplicationController
def index
#category = Category.find(params[:category_id])
#posts = Post.all(:params => {:category_id => #category.id})
respond_to do |format|
format.html
format.xml { render :xml => #posts }
end
end
def show
#category = Category.find(params[:category_id])
#post = Post.find(:id, :params => {:category_id => #category.id})
respond_to do |format|
format.html
format.xml { render :xml => #post }
end
end
end
rake routes:
category_posts GET /categories/:category_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /categories/:category_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_category_post GET /categories/:category_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_category_post GET /categories/:category_id/posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
category_post GET /categories/:category_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /categories/:category_id/posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /categories/:category_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
categories GET /categories(.:format) {:action=>"index", :controller=>"categories"}
POST /categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET /categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT /categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format) {:action=>"destroy", :controller=>"categories"}
post_comments GET /posts/:post_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /posts/:post_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_post_comment GET /posts/:post_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
post_comment GET /posts/:post_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /posts/:post_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /posts/:post_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
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"}
Index works fine, I am able to list posts.
If I do in in console:
Post.find(3, :params=>{:category_id=>2})
I am able to get the desired response but from the browser I get the following error.
Failed. Response code = 404. Response message = Not Found.
try updating your routes to be
resources :categories do
resources :posts do
resources :comments
end
end
then you should be able to access them as nested resources. Not sure where the < Connector is going, is Connector a model based on ActiveModel?
<subjective>
Also, if you are nesting more than one level, you should really take a second look at your application design, as this quickly becomes a headache, see: http://weblog.jamisbuck.org/2007/2/5/nesting-resources (this code is rails 2.x style, but the design issues remain)
</subjective

Resources