ActionController::RoutingError (No route matches {:action=>"show", :controller=>"users", :id=>nil}):

I have been trying to fix this routing error for a long time. I would appreciate any assistance! This error is preventing me from being able to authenticate. Here is what I am getting in my Heroku logs.

app/controllers/authentications_controller.rb:12:in `create'
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"users",      :id=>nil})

Here is the routes.rb file:

Company::Application.routes.draw do
resources :profile_individual
resources :careers
match 'careers' => 'careers#index'
match 'about' => 'about#index'
constraints(:subdomain => /^$|www/) do
devise_for :users
resources :authentications, :identities #, :beta_invitations

resources :users do
  resources :invitations, :controller => 'UserInvitation' do
    post :upload, :on => :collection
    get :email_template, :on => :collection
    get :plaintext_template, :on => :collection
    get :facebook_invitation, :on => :collection
  end
  member do
    get :summary
    get :recruits
    get :friends_events
    get :events_near_me
    get :recent_activity
    get :impact
    get :campaigns
  end
end

resources :password_resets do
  get 'password_reset' => 'password_resets#show', :as => 'password_reset'
end

resources :events, :only => [:new, :index, :create]
resources :organizations, :only => [:index, :create]
resources :orders do
  post :ipn, :on => :member
  resource :payment do
    member do
      post :relay_response
      get :receipt
    end
  end
  resource :paypal_integration do
    member do
      get :authorize
      get :cancel
      post :finalize
    end
  end
end

match '/users/:id/impact/money/:d' => 'users#impact_money_graph', :constraints => {:d =>     /\d+{4}_\d+{2}-\d+{2}/}, :as => :user_impact_money
match '/users/:id/impact/money' => 'users#impact_money_graph', :as => :user_impact_money
match '/users/:id/impact/recruits/:d' => 'users#impact_recruits_graph', :constraints =>     {:d => /\d+{4}_\d+{2}-\d+{2}/}, :as => :user_impact_recruits
match '/users/:id/impact/recruits' => 'users#impact_recruits_graph', :as => :user_impact_recruits

match '/auth/failure' => 'authentications#failure'
match '/auth/:provider/callback' => 'authentications#create'
match '/auth/:provider/callback' => 'authentications#show', :controller => 'users', :as => :login
match '/logout' => 'authentications#destroy', :as => :logout
match '/login' => 'authentications#new', :as => :login
match "/join_team/:id" => "team_members#join", :as => :join_team
match "/rsvp/:id" => "rsvps#show", :as => :rsvp

match "/signup" => 'authentications#signup', :as => :signup

match "/beacon/:id.gif" => "email_beacons#show", :as => :email_beacon
root :to => "homes#show"
match '/corporate_giving' => "homes#corporate_giving"
end

constraints(Subdomain) do
resource :organization, :path => "/", :only => [:edit, :update] do
  member do
    get :org_photos_videos
    get :org_recent_activity
  end
end
resources :events, :except => [:index] do
  post :publish, :on => :member
  resource :supporter_invite
  resource :team_management do
    post :mailer, :on => :member
  end
  resource :team_member do
    post :invite, :on => :member
  end
  resource :rsvp do
    put :make_order, :on => :collection
    get :make_order, :on => :collection
  end
  resources :invites do
    post :upload, :on => :collection
  end
  resources :ticket_tiers, :team_members
end
match "/events" => redirect("/")
root :to => "organizations#show"
end

namespace :admin do
resources :stats
resources :organizations
resources :campaigns do
  resources :rewards
  resources :contents
  put :header, :action => 'header_update'
end
resources :users do
  member do
    post :grant_access
    post :revoke_access
  end
end
resources :nonprofits do
  member do
    put :approve
    put :revoke
  end
end
end

resources :campaigns do
get :find_charities, :on => :collection
get :how_many_charities, :on => :collection
member do
  post :join
  get :join
  post :header, :action => 'header_creation'
  put :header, :action => 'header_update'
end
resources :rewards
resources :contents
resource :donations do
  resource :paypal_integration, :controller => 'donations' do
    member do
      get :authorize
      get :cancel
      post :finalize
    end
  end
end
end

match '/campaigns/:id/graph/:d' => 'campaigns#graph', :constraints => {:d => /\d+{4}_\d+  {2}-\d+{2}/}, :as => :graph_campaign
match '/campaigns/:id/graph' => 'campaigns#graph', :as => :graph_campaign

resources :business_campaigns, :controller => 'campaigns'

resources :businesses do
put :logo, :on => :collection, :action => 'upload_logo'
member do
  get :summary
  get :recruits
  get :friends_events
  get :events_near_me
  get :recent_activity
  get :impact
  get :campaigns
  end
end

resources :nonprofit_campaigns, :controller => 'campaigns'

resources :nonprofits do
put :logo, :on => :collection, :action => 'upload_logo'
member do
  get :summary
  get :recruits
  get :friends_events
  get :events_near_me
  get :recent_activity
  get :impact
  get :campaigns
  get :supporting_campaigns
 end
end

resources :publicities

match '/campaigns/:campaign_id/rewards/:id' => 'campaigns#reward', :via => :get

match "/robots.txt" => "application#robots_txt"
match "/beta_invitations" => redirect('/')
resource :sitemap
resources :referrals

end

Here is my authentications_controller.rb file

class AuthenticationsController < ApplicationController
  skip_before_filter :require_beta_access
  before_filter :redirect_to_profile_if_logged_in, :only => [:create, :new]

  layout :resolve_layout

  def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'],  omniauth['uid'])
    if authentication && authentication.user.present?
      sign_in(:user, authentication.user)
      redirect_to session[:redirect_to] || user_path(current_user, :subdomain => nil)
    elsif current_user
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
      redirect_to session[:redirect_to] || user_path(current_user, :subdomain => nil)
    else
      user = User.new  
      user.apply_omniauth(omniauth)

      logger.debug "=======================auth============================="
      logger.debug session[:referrer_token]
      logger.debug "========================================================"

      if session[:referrer_token]
        publicity = Publicity.find_by_token(session[:referrer_token]) 
        user.invited_by = publicity
        user.recruited_by = publicity
      end

      if user.save
        sign_in(user)

        unless session[:redirect_to]
          session[:referrer_token] = nil
        end

        redirect_to session[:redirect_to] || user_path(current_user, :subdomain => nil)
        #redirect_to session[:redirect_to] || campaigns_url(:tc => request.env['omniauth.params']['tc']) #tc is for AB testing
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to signup_path
      end
    end
  end

  def failure
    flash[:error] = "Please check your email and password and try again"
    redirect_to login_path
  end

  def destroy
    reset_session
    redirect_to root_path
  end

  def signup
    #
  end

  private

  def redirect_to_profile_if_logged_in
    redirect_to user_path(current_user.permalink) if current_user
  end

  def resolve_layout
    case action_name
    when "new", "signup"
      "authentication"
    else
      "selfcontained"
    end
  end
end

I am adding my appplication_controller.rb too:

class ApplicationController < ActionController::Base
  #Wrote by George for beta users -before_filter :require_beta_access 
  before_filter :save_referrer_token
  protect_from_forgery
  helper_method :organization_admin?, :team_member?, :profile_url, :current_profile

  def set_headers
    # Set our headers here
  end

  def save_referrer_token
    #session.delete(:referrer_token)
    if params[:ref]
      publicity = Publicity.find_by_token(params[:ref])
      logger.debug "========================================================"
      logger.debug current_profile.nil?
      logger.debug publicity.creator
      logger.debug current_profile
      logger.debug current_profile != publicity.creator 
      session[:referrer_token] = params[:ref] if current_profile.nil? or publicity.creator != current_profile

      logger.debug session[:referrer_token]
      logger.debug "========================================================"
    end 
  end

  def robots_txt
    robots = File.read(Rails.root + "public/robots.#{Rails.env}.txt")
    render :text => robots, :layout => false, :content_type => "text/plain"
  end

  def load_organization
    @organization = Organization.find_by_permalink(request.subdomain)
    raise ActiveRecord::RecordNotFound if @organization.nil?
  end

  def require_user
    unless current_user
      session[:redirect_to] = request.url
      redirect_to login_url(:host => request.domain)
    end
  end

  def require_beta_access
    if !current_user
      redirect_to root_url(:host => request.domain)
    elsif !current_user.beta_access?
      redirect_to new_beta_invitation_url(:host => request.domain)
    end
  end

  def require_organization_admin
    unless organization_admin?
      redirect_to root_url(:subdomain => @organization.permalink)
    end
  end

  def team_member?
    if current_user && @event.team_memberships.where(:user_id => current_user.id).count != 0
      true
    end
  end

  def organization_admin?
    if current_user && current_user.beta_access? && @organization &&  @organization.memberships.where(:user_id => current_user.id, :role => 'admin').count != 0
      true
    end
  end

  def profile_url(profile, opt = nil)
    if profile == current_user 
       user_url(profile, :host => opt[:host])
    elsif profile.is_a? BusinessProfile
      business_url(profile)
    elsif profile.is_a? NonprofitProfile
      nonprofit_url(profile)
    end
  end

  def set_current_profile(profile)
    session[:current_profile] = profile
  end

  def current_user
    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
  end

  def current_profile
    #if session
      session[:current_profile] || current_user
    #else
    #  nil
    #end
  end

  IGIVEMORE_HTML5_OPTIOINS = {
    :style => 'z-index: 0;',:width => '290', :height => '200', 
    :frameborder => '0', :url_params => {:wmode=>"opaque"}
  }

  def campaign_header_body(camp, opt = IGIVEMORE_HTML5_OPTIOINS)
    if camp.header_type == Campaign::HEADER_YOUTUBE
      youtube_html5(camp.header_url, opt).html_safe
    elsif camp.header_type == Campaign::HEADER_IMAGE
      "<img src=\"#{camp.header_url}\" width=\"#{opt[:width]}\" height=\"#{opt[:height]}\"/>'".html_safe
    else
      "Unsupported Type!!"
    end
  end  

  def youtube_html5(url, opt)
    begin
      video = YouTubeIt::Client.new.video_by(url)
      video.embed_html5(opt).gsub(/http:\/\//,"https://")
    rescue => e
      "<div style='color:red; width:290px; height:100px; padding-top:100px'>Given Video URL has problem.</div>"
    end
  end
end

Answers


The problem is that current_user is returning nil in line 12 of authentications_controller.rb:

11    sign_in(:user, authentication.user)
12    redirect_to session[:redirect_to] || user_path(current_user, :subdomain => nil)

The sign_in method which is called directly before this should set cookies[:auth_token] so that the current_user method can retrieve it:

def current_user
  @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end

I can't see your sign_in method, but make sure that you are setting cookies[:auth_token] correctly.


Need Your Help

using domain name in localhost

php apache localhost

i have problem to using domain name in localhost