Rails & Devise: Two-step confirmation route error - ruby-on-rails

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.

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.

Routing Form to Update action?

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

I'm gettgin No route matches {:action=>"new", :controller=>"posts"} even though it's not in my routes.rb file. HELP?

OK so this is my first question posted to stackoverflow. I'm pretty new to coding, been learning Ruby on Rails for the past 3 months. There could be a few things wrong with my code but here it goes.
Basically I'm trying to get a User to Post. I'm using Devise for registration, and all of that works. But when I create a link to "Create Post" in my Header View, it tells me I don't have a route matching => even though I think it exists. I'm missing something small I believe but in all the debugging I've done to try and get it right, I think I might have messed something else up along the way. Below is attached my code for the routes.rb, my post_controller file, and my layout view file. Sorry about all the rambling, I couldn't be very concise. Does anyone see anything wrong? Let me know if you need to see other code
_header.html.erb
<% if user_signed_in? %>
Signed in as <%= current_user.username %>. Not you?
<%= link_to "Logout", destroy_user_session_path, :method => :delete, %>
<%= link_to "Create Post", new_user_post_path %>
<%= link_to "Search", posts_index_path %>
<%= link_to "Show All", posts_show_path %>
routes.rb
#devise_for :users
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
match '/users/:user_id/posts/new', :to => 'posts#new'
resources :users do
resources :posts, :only => [:new, :create, :show, :index, :destroy]
end
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/safety', :to => 'pages#safety'
match '/privacy', :to => 'pages#privacy'
get 'posts/index'
get 'posts/show'
get 'posts/post'
match 'posts/search', :to => 'posts#search'
root :to => 'pages#home'
posts_controller
def create
if signed_in?
#user = current_user.posts.build(params[:user][:post])
if #user.save
flash[:success] = "Thanks for creating your post! " +
redirect_to new_user_post_path(#post)
else
render 'new'
def new
#title = "Create Post"
#post = Post.new
end
rake routes
users_sign_out GET /users/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
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"}
/users/:user_id/posts/new(.:format) {:controller=>"posts", :action=>"new"}
user_posts GET /users/:user_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /users/:user_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_user_post GET /users/:user_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
user_post GET /users/:user_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
DELETE /users/:user_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
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"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
safety /safety(.:format) {:controller=>"pages", :action=>"safety"}
privacy /privacy(.:format) {:controller=>"pages", :action=>"privacy"}
posts_index GET /posts/index(.:format) {:controller=>"posts", :action=>"index"}
posts_show GET /posts/show(.:format) {:controller=>"posts", :action=>"show"}
posts_post GET /posts/post(.:format) {:controller=>"posts", :action=>"post"}
posts_search /posts/search(.:format) {:controller=>"posts", :action=>"search"}
root /(.:format) {:controller=>"pages", :action=>"home"}
Try typing rake routes in the console and check if the route you are looking for exists. Note that the order also matters.
It should have been
<%= link_to "Create Post", new_user_post_path(current_user) %>
I think its because of the argument !
In your routes file it seems you are passing :user_id also.
In your link try passing that id.
Hope it helps...
<%= link_to "create Post", new_user_post_path(current_user.id) %>
Also I would suggest try renaming the path and use it.
I know its not proper way but still.
I had faced a similar problem and solved by doing this !!;)
Hope it helps !

Rails singular resource does't seem to generate 'user_path'

I have the following routes in my 3.1.0.rc5 app
# config/routes.rb
devise_for :users
resources :users, only: :index
resource :user
THe idea here is that I use devise for session management, the 'users' resource just to generate the users_path for the index action and then most other user actions would be accessible via routes like
GET user_path -> show action
GET new_user_path -> new action
POST user_path -> create action
The user_path route helper doesn't seem to be generated though, whenever I try to use it in a view, I get a weird error when rails tries to render it.
For example, on the /user/new page, I have the following
<%= form_for #user, :url => user_path do |f| %>
# omitted form elements
<% end %>
When rails tries to render the page I get
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"users"}):
7: </div>
8:
9: <div class="content_middle">
10: <%= form_for #user, :url => user_path do |f| %>
11: <fieldset>
12: <%= render partial: "form_errors" %>
13:
app/views/users/new.html.erb:10:in `_app_views_users_new_html_erb___1548382046039026466_2191201580'
What's up with that!?
Edit Here is the content of rake routes. It's pretty massive so I cut it down to just the user related 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"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
user DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
POST /user(.:format) {:action=>"create", :controller=>"users"}
new_user GET /user/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /user/edit(.:format) {:action=>"edit", :controller=>"users"}
GET /user(.:format) {:action=>"show", :controller=>"users"}
PUT /user(.:format) {:action=>"update", :controller=>"users"}
It might make sense to have a Profile or CurrentUser controller in this situation. It would eliminate any routing conflicts you may be having with Devise, and also, it makes sense from a RESTful point of view as you're treating the current user as a distinct resource.
devise_for :user
resources :users, :only => :index
resource :profile

Why isn't this link to tag linking to the correct model?

So after pulling my hair out for an hour, I decided to post this here. I get this error:
NoMethodError in Articles#show
Showing /home/leon/sites/VIS/app/views/articles/_article.html.erb where line #3 raised:
undefined method `user_path' for #<#<Class:0x7fdd81fe1eb8>:0x7fdd81fdfdc0>
Extracted source (around line #3):
1: <div id="left-column">
2: <p class="label-b">Author<br>
3: <%= link_to "#{#article.user.penname}'s profile", user_path(article.user) %>
user_path(article.user) %>
When I try to link to the article's authoring user. My routes file:
resources :tags
get "admin/index"
get "admin/show"
get "articles/contact"
get "articles/about"
resources :roles
devise_for :users, :controllers => { :registrations => "users/registrations" }
resources :articles do
resources :comments
end
I do have a belongs_to :user in my article model file and a has_many :articles in my user model file. I'm using this link to link to the author's profile within the article.
Please help! I'm using can can for my permission management and devise for my authentication, but its throwing the error within the show action of the article so I didn't post that code. Let me know if I should. Thanks!
#Jacob
I get the same error:
NoMethodError in Articles#show
Showing /home/leon/sites/VIS/app/views/articles/_article.html.erb where line #3 raised:
undefined method `user_path' for #<#:0x7fdd82217a98>
Extracted source (around line #3):
1:
2: Author
3: <%= link_to "#{#article.user.penname}'s profile", article.user %>
4: Previous Versions
5: Version 7
6: <%= image_tag "add-to-favorites.png", :class => "favorites-button" %>
The relevant part of my routes:
{:action=>"destroy", :controller=>"roles"}
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 GET /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"}
user_password PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"users/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"users/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"users/registrations"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"users/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"users/registrations"}
user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /users/confirmation/new(.:format) {:action=>"new", :controller=>"devise/confirmations"}
user_confirmation GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"}
Aha! It doesn't have a route for user#show, but i'm not sure why as I have:
class UsersController < ApplicationController
...
def show
respond_to do |format|
format.json { render :json => #user }
format.xml { render :xml => #user }
format.html
end
rescue ActiveRecord::RecordNotFound
respond_to_not_found(:json, :xml, :html)
end
in my app/controllers folder. Do I need to move my users controller into app/controllers/devise and overload it with devise instead of application controller?
You can do:
<%= link_to "#{#article.user.penname}'s profile", article.user %>
And Rails will automatically determine the route.
As seen here:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Update
Yes, you are going to have to inherit from the DeviseController rather than the ApplicationController. From here you can add your own custom actions, such as show(which is what you doing now).
Look at: https://github.com/plataformatec/devise
and search for: Configuring controllers
Try article.user instead of user_path(article.user). If that doesn't work, can you post the output from rake routes?
I figured it out. After playing around with the routes for a while and reading the rails documentation, I added this line to my routes file:
devise_for :users, :controllers => { :registrations => "users/registrations" }
resources :users, :only => [:index, :show] <== THIS LINE
below my devise_for routes. This allows me to display the user profiles without having to worry about the edit update etc. roles that devise provides for. Thanks for the answers anyway guys, I appreciate it!
Shouldn't it just be: #article.user instead of article.user ??
<%= link_to "#{#article.user.penname}'s profile", #article.user %>
Why would you use an instance variable for the user penname and a local variable for the user object? I think that's your problem.

Resources