Routing Form to Update action? - ruby-on-rails

I have a custom 'trackupload' view (that's really just a variation on my edit view). I'm rendering this with the following controller action:
def trackupload
#release = Release.find(params[:release_id]) #Note: I had to use :release_id instead of :id, not sure why?
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #release }
format.pdf { render :layout => false }
end
end
And my update action:
def update
#release = Release.find(params[:id])
respond_to do |format|
if #release.update_attributes(params[:release])
format.html { redirect_to #release, :notice => 'Release was successfully updated.' }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => #release.errors, :status => :unprocessable_entity }
end
end
end
The form itself is as follows:
<%= form_for(#release, :action => "update", :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= #release.title %></h3>
<%= f.fields_for :tracks do |builder| %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>
<%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>
All I want to do it make this use the release controller's update action on submit. I thought this would a simple case of adding action => "update" to my form_for tag but it doesn't seem to work. I get no error, but nothing happens.
Any idea what's missing or where i'm going wrong?
Output of Rake Routes for Releases:
autocomplete_artist_name_releases GET /releases/autocomplete_artist_name(.:format) {:action=>"autocomplete_artist_name", :controller=>"releases"}
release_artists GET /releases/:release_id/artists(.:format) {:action=>"index", :controller=>"artists"}
POST /releases/:release_id/artists(.:format) {:action=>"create", :controller=>"artists"}
new_release_artist GET /releases/:release_id/artists/new(.:format) {:action=>"new", :controller=>"artists"}
edit_release_artist GET /releases/:release_id/artists/:id/edit(.:format) {:action=>"edit", :controller=>"artists"}
release_artist GET /releases/:release_id/artists/:id(.:format) {:action=>"show", :controller=>"artists"}
PUT /releases/:release_id/artists/:id(.:format) {:action=>"update", :controller=>"artists"}
DELETE /releases/:release_id/artists/:id(.:format) {:action=>"destroy", :controller=>"artists"}
release_tracks GET /releases/:release_id/tracks(.:format) {:action=>"index", :controller=>"tracks"}
POST /releases/:release_id/tracks(.:format) {:action=>"create", :controller=>"tracks"}
new_release_track GET /releases/:release_id/tracks/new(.:format) {:action=>"new", :controller=>"tracks"}
edit_release_track GET /releases/:release_id/tracks/:id/edit(.:format) {:action=>"edit", :controller=>"tracks"}
release_track GET /releases/:release_id/tracks/:id(.:format) {:action=>"show", :controller=>"tracks"}
PUT /releases/:release_id/tracks/:id(.:format) {:action=>"update", :controller=>"tracks"}
DELETE /releases/:release_id/tracks/:id(.:format) {:action=>"destroy", :controller=>"tracks"}
release_trackupload GET /releases/:release_id/trackupload(.:format) {:action=>"trackupload", :controller=>"releases"}
release_releases_tracks GET /releases/:release_id/releases_tracks(.:format) {:action=>"index", :controller=>"releases_tracks"}
POST /releases/:release_id/releases_tracks(.:format) {:action=>"create", :controller=>"releases_tracks"}
new_release_releases_track GET /releases/:release_id/releases_tracks/new(.:format) {:action=>"new", :controller=>"releases_tracks"}
edit_release_releases_track GET /releases/:release_id/releases_tracks/:id/edit(.:format) {:action=>"edit", :controller=>"releases_tracks"}
release_releases_track GET /releases/:release_id/releases_tracks/:id(.:format) {:action=>"show", :controller=>"releases_tracks"}
PUT /releases/:release_id/releases_tracks/:id(.:format) {:action=>"update", :controller=>"releases_tracks"}
DELETE /releases/:release_id/releases_tracks/:id(.:format) {:action=>"destroy", :controller=>"releases_tracks"}
release_product_tracks GET /releases/:release_id/products/:product_id/tracks(.:format) {:action=>"index", :controller=>"tracks"}
POST /releases/:release_id/products/:product_id/tracks(.:format) {:action=>"create", :controller=>"tracks"}
new_release_product_track GET /releases/:release_id/products/:product_id/tracks/new(.:format) {:action=>"new", :controller=>"tracks"}
edit_release_product_track GET /releases/:release_id/products/:product_id/tracks/:id/edit(.:format) {:action=>"edit", :controller=>"tracks"}
release_product_track GET /releases/:release_id/products/:product_id/tracks/:id(.:format) {:action=>"show", :controller=>"tracks"}
PUT /releases/:release_id/products/:product_id/tracks/:id(.:format) {:action=>"update", :controller=>"tracks"}
DELETE /releases/:release_id/products/:product_id/tracks/:id(.:format) {:action=>"destroy", :controller=>"tracks"}
release_product_producttracklistings GET /releases/:release_id/products/:product_id/producttracklistings(.:format) {:action=>"index", :controller=>"producttracklistings"}
POST /releases/:release_id/products/:product_id/producttracklistings(.:format) {:action=>"create", :controller=>"producttracklistings"}
new_release_product_producttracklisting GET /releases/:release_id/products/:product_id/producttracklistings/new(.:format) {:action=>"new", :controller=>"producttracklistings"}
edit_release_product_producttracklisting GET /releases/:release_id/products/:product_id/producttracklistings/:id/edit(.:format) {:action=>"edit", :controller=>"producttracklistings"}
release_product_producttracklisting GET /releases/:release_id/products/:product_id/producttracklistings/:id(.:format) {:action=>"show", :controller=>"producttracklistings"}
PUT /releases/:release_id/products/:product_id/producttracklistings/:id(.:format) {:action=>"update", :controller=>"producttracklistings"}
DELETE /releases/:release_id/products/:product_id/producttracklistings/:id(.:format) {:action=>"destroy", :controller=>"producttracklistings"}
release_product_itunes_data GET /releases/:release_id/products/:product_id/itunes_data(.:format) {:action=>"index", :controller=>"itunes_data"}
POST /releases/:release_id/products/:product_id/itunes_data(.:format) {:action=>"create", :controller=>"itunes_data"}
new_release_product_itunes_datum GET /releases/:release_id/products/:product_id/itunes_data/new(.:format) {:action=>"new", :controller=>"itunes_data"}
edit_release_product_itunes_datum GET /releases/:release_id/products/:product_id/itunes_data/:id/edit(.:format) {:action=>"edit", :controller=>"itunes_data"}
release_product_itunes_datum GET /releases/:release_id/products/:product_id/itunes_data/:id(.:format) {:action=>"show", :controller=>"itunes_data"}
PUT /releases/:release_id/products/:product_id/itunes_data/:id(.:format) {:action=>"update", :controller=>"itunes_data"}
DELETE /releases/:release_id/products/:product_id/itunes_data/:id(.:format) {:action=>"destroy", :controller=>"itunes_data"}
release_products GET /releases/:release_id/products(.:format) {:action=>"index", :controller=>"products"}
POST /releases/:release_id/products(.:format) {:action=>"create", :controller=>"products"}
new_release_product GET /releases/:release_id/products/new(.:format) {:action=>"new", :controller=>"products"}
edit_release_product GET /releases/:release_id/products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
release_product GET /releases/:release_id/products/:id(.:format) {:action=>"show", :controller=>"products"}
PUT /releases/:release_id/products/:id(.:format) {:action=>"update", :controller=>"products"}
DELETE /releases/:release_id/products/:id(.:format) {:action=>"destroy", :controller=>"products"}
releases GET /releases(.:format) {:action=>"index", :controller=>"releases"}
POST /releases(.:format) {:action=>"create", :controller=>"releases"}
new_release GET /releases/new(.:format) {:action=>"new", :controller=>"releases"}
edit_release GET /releases/:id/edit(.:format) {:action=>"edit", :controller=>"releases"}
release GET /releases/:id(.:format) {:action=>"show", :controller=>"releases"}
PUT /releases/:id(.:format) {:action=>"update", :controller=>"releases"}
DELETE /releases/:id(.:format) {:action=>"destroy", :controller=>"releases"}

You can remove :action => 'update'.
By default persisted record directs to this action
form_for(#release, :html => { :multipart => true }) do |f|

Try change your action => "update" to be inside the ":url" attribute:
:url => { :action => "update" }

Related

Ruby on Rails Class Variable not being referenced by controller

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.

Rails: How to make a custom route from resource

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.

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

Rails 3 Form For Custom Action

I'm having trouble routing a form to a custom action in Rails 3. Here are my routes:
resources :photos do
resources :comments
collection do
get 'update_states'
end
member do
put 'upload'
end
end
Here's the form_for:
form_for #photo, :remote => true, :url => { :action => upload_photo_path(#photo) }, :html => { :multipart => :true, :method => 'put' } do |f|
And here's the error message:
No route matches {:action=>"/photos/42/upload", :controller=>"photos"}
... this is especially frustrating because "photos/:id/upload" is exactly the correct action for this form.
What am I missing?
EDITS - Here are the original Photo-related routes:
photo_comments GET /photos/:photo_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /photos/:photo_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_photo_comment GET /photos/:photo_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
photo_comment GET /photos/:photo_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /photos/:photo_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /photos/:photo_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
update_states_photos GET /photos/update_states(.:format) {:action=>"update_states", :controller=>"photos"}
upload_photo PUT /photos/:id/upload(.:format) {:action=>"upload", :controller=>"photos"}
photos GET /photos(.:format) {:action=>"index", :controller=>"photos"}
POST /photos(.:format) {:action=>"create", :controller=>"photos"}
new_photo GET /photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_photo GET /photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
photo GET /photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
Here are the relevant routes when I changed the route to match 'upload':
photo_comments GET /photos/:photo_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /photos/:photo_id/comments(.:format) {:action=>"create", :controller=>"comments"}
}
new_photo_comment GET /photos/:photo_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
photo_comment GET /photos/:photo_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /photos/:photo_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /photos/:photo_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
update_states_photos GET /photos/update_states(.:format) {:action=>"update_states", :controller=>"photos"}
upload_photo /photos/:id/upload(.:format) {:action=>"upload", :controller=>"photos"}
photos GET /photos(.:format) {:action=>"index", :controller=>"photos"}
POST /photos(.:format) {:action=>"create", :controller=>"photos"}
new_photo GET /photos/new(.:format) {:action=>"new", :controller=>"photos"}
edit_photo GET /photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
photo GET /photos/:id(.:format) {:action=>"show", :controller=>"photos"}
PUT /photos/:id(.:format) {:action=>"update", :controller=>"photos"}
DELETE /photos/:id(.:format) {:action=>"destroy", :controller=>"photos"}
Unfortunately 'match' didn't work any better...
-- EDIT --
Just to confirm another scenario here... with this in the routes:
resources :photos do
resources :comments
collection do
get 'update_states'
end
member do
match 'upload'
end
end
and this in the view:
form_for #photo, :remote => true, :url => { :action => 'upload' }, :html => { :multipart => :true, :id => 'photo_upload' } do |f|
I still get:
No route matches {:action=>"upload", :controller=>"photos"}
What if you did just :url => upload_photo_path(#photo)?
It seems a little strange that you'd be uploading to a member though. Is this just a creation method (in which case you should just POST to the collection path)?
I had the same problem and finally worked through to a solution which I'm not sure was reached in the above case, since the original poster moved on to another approach.
My routes had
resources :members do
member do
get "invite"
post 'register'
end
end
And "rake routes" included
register_member POST /members/:id/register(.:format) {:protocol=>"http", :action=>"register", :controller=>"members"}
Yet I kept getting the error
Started POST "/members/149/register" for 127.0.0.1 at 2012-04-13 13:18:35 -0700
ActionController::RoutingError (No route matches "/members/149/register"):
Rendered /Users/lisa/.rvm/gems/ruby-1.9.2-p180#stv/gems/actionpack-3.0.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.1ms)
and the problem was limited only to the form generated by Rails according to the below form_for (note I confirmed this using HTTP client to manually POST to the URL and saw it was finding the route)
<%= form_for #account, :url => register_member_path(#account.id) do |account_form| %>
...
I checked the method, I checked the path, everything looked good. What I finally noticed, scouring the generated form line by line:
<form accept-charset="UTF-8" action="/members/149/register" class="edit_member" id="edit_member_149" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="74pkMgRHfdowSfzjJGMILkAsivVNrJZ0iWYXRUgxyF0=" />
</div>
...
Notice the hidden input name="_method". I wish the fact that Rails was interpreting this as a PUT had shown up in the logs, that would have made my debugging a lot faster. I fixed it by telling the form explicitly to use the POST method, which of course it was already, but telling it that removed the hidden _method override. I assume there's some facet about #account which triggered Rails to use the _method parameter, but #account should be an existing record.
Your url parameter should be
:url => { :action => "upload" }
(Original reply)
I would bet it's because your route expects a PUT and your form is sending a POST (probably because #photo = Photo.new). You have several options:
Change your route to post 'upload'
Create your form with form_for #photo, :as => :post with the rest of your arguments
Make sure #photo is an existing record (e.g. by calling create instead of new)
The most appropriate choice is probably one of the first 2.

Resources