2015-05-18 32 views
1

我得到错误:ActionController :: UrlGenerationError在主页#home没有路由我atches {:action =>“edit”,:controller =>“users”,:id => nil}缺少必需的键:[:id]

ActionController::UrlGenerationError in Home#home No route matches {:action=>"edit", :controller=>"users", :id=>nil} missing required keys: [:id] 

在我家的控制器。当我登录,我在家里/家庭得到错误主要是关于

No route matches...:controller=>"users", :id=>nil} missing required keys: [:id]. 

出于某种原因,它看起来像sign_in不工作或CURRENT_USER是零。

有关如何解决此错误的任何想法?我正在使用设计和omniauth。

这里是我的users_model:

class User < ActiveRecord::Base 
TEMP_EMAIL_PREFIX = '[email protected]' 
TEMP_EMAIL_REGEX = /\[email protected]/ 
# Include default devise modules. Others available are: 
# :lockable, :timeoutable 
devise :database_authenticatable, :registerable, :confirmable, 
:recoverable, :rememberable, :trackable, :validatable, :omniauthable 
validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update 
def self.find_for_oauth(auth, signed_in_resource = nil) 
# Get the identity and user if they exist 
identity = Identity.find_for_oauth(auth) 
# If a signed_in_resource is provided it always overrides the existing user 
# to prevent the identity being locked with accidentally created accounts. 
# Note that this may leave zombie accounts (with no associated identity) which 
# can be cleaned up at a later date. 
user = signed_in_resource ? signed_in_resource : identity.user 

# Create the user if needed 
if user.nil? 

# Get the existing user by email if the provider gives us a verified email. 
# If no verified email was provided we assign a temporary email and ask the 
# user to verify it on the next step via UsersController.finish_signup 
email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email) 
email = auth.info.email if email_is_verified 
user = User.where(:email => email).first if email 

# Create the user if it's a new registration 
if user.nil? 
user = User.new(
    name: auth.extra.raw_info.name, 
    #username: auth.info.nickname || auth.uid, 
    email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com", 
    password: Devise.friendly_token[0,20] 
) 
    user.skip_confirmation! 
    user.save! 
    end 
    end 

# Associate the identity with the user if needed 
    if identity.user != user 
    identity.user = user 
    identity.save! 
    end 
user 
end 

def email_verified? 
self.email && self.email !~ TEMP_EMAIL_REGEX 
end 
end 

这里是我的共享/ user_info.html.erb:

<h1><%= current_user.email %></h1> 
<span><%= link_to "view my profile", current_user %></span> 
<span><%= pluralize(current_user.posts.count, "post") %></span> 
<%end%> 

这里是我的应用程序/视图/布局/ header.html.erb:

<header class="navbar navbar-fixed-top navbar-inverse"> 
<div class="container"> 
<%= link_to "UNSTARV ALPHA", '#', id: "logo" %> 
<nav> 
    <ul class="nav navbar-nav navbar-right"> 
    <li><%= link_to "Home", root_path %></li> 
    <li><%= link_to "Groups", groups_path %></li> 
    <% if signed_in? %> 
     <li><%= link_to "Users", users_path %></li> 
     <li class="dropdown"> 
     <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
      Account <b class="caret"></b> 
     </a> 
     <ul class="dropdown-menu"> 
      <li><%= link_to "Profile", current_user %></li> 
      <li><%= link_to "Settings", edit_user_path(current_user) %></li> 
      <li class="divider"></li> 
      <li> 
      <%= link_to "Log out", logout_path, method: "delete" %> 
      </li> 
     </ul> 
     </li> 
     <% else %> 
     <li><%= link_to "Log in", login_path %></li> 
     <li><%= link_to "Sign up", signup_path %></li> 
     <% end %> 

     </div></li> 
     </ul> 
     </nav> 
      </div> 

这里是我的routes.rb:

Rails.application.routes.draw do 

    get 'groups/new' 

    get 'groups/create' 

    get 'groups/show' 

    get 'groups/edit' 

    get 'groups/update' 

    get 'groups/destroy' 
    get 'groups' => 'groups#index' 
    get 'groups/new' 
    get 'signup' =>'users#new' 
    get 'rooms' =>'home#rooms' 

    match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup 


    devise_for :users, :controllers => { omniauth_callbacks: 'omniauth_callbacks' } 

    resources :users do 
    member do 
    get :following, :followers 
    end 
    end 


    root 'home#home' 
    get 'terms' => 'home#terms' 
    get 'privacy' => 'home#privacy' 
    get 'about' => 'home#about' 

    get 'login' => 'sessions#new' 
    post 'login' => 'sessions#create' 
    delete 'logout' => 'sessions#destroy' 

    resources :posts do 
    member { post :vote } 
    resources :comments do 
    member { post :vote } 
    end 
    end 

    resources :groups 
    resources :relationships,  only: [:create, :destroy] 

这里是我的全部跟踪:

actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:219:in `raise_generation_error' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:192:in `optimized_helper' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:178:in `call' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:270:in `block (2 levels) in define_url_helper' 
app/views/layouts/_header.html.erb:20:in `_app_views_layouts__header_html_erb__4827727_36507960' 
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render' 
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument' 
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument' 
actionview (4.1.8) lib/action_view/template.rb:143:in `render' 
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial' 
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:279:in `block in render' 
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument' 
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument' 
activesupport (4.1.8)lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument' 
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument' 
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:278:in `render' 
actionview (4.1.8) lib/action_view/renderer/renderer.rb:47:in `render_partial' 
actionview (4.1.8) lib/action_view/helpers/rendering_helper.rb:35:in `render' 
app/views/layouts/application.html.erb:14:in `_app_views_layouts_application_html_erb__798221577_35492700' 
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render' 
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument' 
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument' 
actionview (4.1.8) lib/action_view/template.rb:143:in `render' 
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:67:in `render_with_layout' 
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:53:in `render_template' 
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:17:in `render' 
actionview (4.1.8) lib/action_view/renderer/renderer.rb:42:in `render_template' 
actionview (4.1.8) lib/action_view/renderer/renderer.rb:23:in `render' 
actionview (4.1.8) lib/action_view/rendering.rb:99:in `_render_template' 
actionpack (4.1.8) lib/action_controller/metal/streaming.rb:217:in `_render_template' 
actionview (4.1.8) lib/action_view/rendering.rb:82:in `render_to_body' 
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:32:in `render_to_body' 
actionpack (4.1.8) lib/action_controller/metal/renderers.rb:32:in `render_to_body' 
actionpack (4.1.8) lib/abstract_controller/rendering.rb:25:in `render' 
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:16:in `render' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render' 
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `block in ms' 
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/benchmark.rb:296:in `realtime' 
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `ms' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block in render' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime' 
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:40:in `render' 
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:10:in `default_render' 
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:5:in `send_action' 
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action' 
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action' 
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting' 
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks' 
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' 
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument' 
activesupport (4.1.8)lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument' 
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action' 
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action' 
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process' 
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process' 
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch' 
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' 
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call' 
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call' 
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each' 
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call' 
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call' 
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!' 
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call' 
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call' 
warden (1.2.3) lib/warden/manager.rb:35:in `block in call' 
warden (1.2.3) lib/warden/manager.rb:34:in `catch' 
warden (1.2.3) lib/warden/manager.rb:34:in `call' 
rack (1.5.2) lib/rack/etag.rb:23:in `call' 
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call' 
rack (1.5.2) lib/rack/head.rb:11:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call' 
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' 
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call' 
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call' 
activerecord 
(4.1.8)lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call' 
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks' 
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app' 
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call' 
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged' 
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged' 
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
activesupport (4.1.8)  lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call' 
rack (1.5.2) lib/rack/sendfile.rb:112:in `call' 
railties (4.1.8) lib/rails/engine.rb:514:in `call' 
railties (4.1.8) lib/rails/application.rb:144:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
rack (1.5.2) lib/rack/content_length.rb:14:in `call' 
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' 
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service' 
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run' 
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread' 

这里是我的omniauth_callbacks_controller.rb:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
def self.provides_callback_for(provider) 
class_eval %Q{ 
    def #{provider} 
    @user = User.find_for_oauth(env["omniauth.auth"], current_user) 

    if @user.persisted? 
     sign_in_and_redirect @user, event: :authentication 
     set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? 
    else 
     session["devise.#{provider}_data"] = env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
    } 
end 

[:twitter, :facebook, :linked_in].each do |provider| 
provides_callback_for provider 
end 

def after_sign_in_path_for(resource) 
#if resource.email_verified? 
    super resource 
    current_user = @user 
#else 
# finish_signup_path(resource) 
#end 
end 
end 

我的用户控制器:

class UsersController < ApplicationController 
before_action :set_user, only: [:show, :edit, :update, :destroy, :finish_signup] 

def index 
@users = User.all 
end 

# GET /users/:id.:format 
def show 
# authorize! :read, @user 

end 

# GET /users/:id/edit 
def edit 
# authorize! :update, @user 
end 
def new 
@user = User.new 
end 

# PATCH/PUT /users/:id.:format 
def update 
# authorize! :update, @user 
respond_to do |format| 
    if @user.update(user_params) 
    sign_in(@user == current_user ? @user : current_user, :bypass => true) 
    format.html { redirect_to @user, notice: 'Your profile was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: 'edit' } 
    format.json { render json: @user.errors, status: :unprocessable_entity } 
    end 
    end 
    end 

    # GET/PATCH /users/:id/finish_signup 
    def finish_signup 
    # authorize! :update, @user 
    if request.patch? && params[:user] #&& params[:user][:email] 
    if @user.update(user_params) 
    @user.skip_reconfirmation! 
    sign_in(@user, :bypass => true) 
    redirect_to @user, notice: 'Your profile was successfully updated.' 
    else 
    @show_errors = true 
    end 
    end 
    end 

    # DELETE /users/:id.:format 
    def destroy 
    # authorize! :delete, @user 
    @user.destroy 
    respond_to do |format| 
    format.html { redirect_to root_url } 
    format.json { head :no_content } 
    end 
    end 
    def set_user 
    @user = User.find(params[:id]) 
    end 

    def following 
    @title = "Following" 
    @user = User.find(params[:id]) 
    @users = @user.following.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    def followers 
    @title = "Followers" 
    @user = User.find(params[:id]) 
    @users = @user.followers.paginate(page: params[:page]) 
    render 'show_follow' 
    end 



    private 
    def user_params 
    accessible = [ :name, :email ] # extend with your own params 
    accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank? 
    params.require(:user).permit(:database_authenticatable, :registerable, #:confirmable, 
:recoverable, :rememberable, :trackable, :validatable, :omniauthable,:encrypted_password, #:confirmed_at) 
end 
end 
+0

向我们显示您的config.routes.rb文件或运行'rake routes'。显示你在哪里和/或你如何调用这个动作。我最好的猜测是你在调用这个动作时不会传递用户标识。像这样''<%= link_to“编辑帐户”,edit_user_path(@user),class:'btn btn-success'%>'在你的代码中你可能会缺少@user。 –

+0

显示'app/views/layouts/_header.html.erb'第20行。 – AbM

+0

@LuisMenjivar我添加了更多信息 – codigomonstruo

回答

1

我不是太熟悉Omniauth说实话,我不知道这是否是正确的答案,但这里是我的2美分。

我在想也许你的<li><%= link_to "Settings", edit_user_path(current_user) %></li>没有从current_user得到id。尝试删除该行以查看错误是否消失。 也许当用户使用omniauth登录时,它不会创建用户,并且只有在用户验证其电子邮件时才创建用户,或者只有在Facebook/Twitter/whatnot中的电子邮件有效时才创建用户。所以当你调用current_user时,current_user不存在。

使用omniauth使用有效的(facebook/twitter/whatnot)电子邮件登录,然后检查数据库以查看它是否创建了用户。

尝试<li><%= link_to "Settings", edit_user_path(@user) %></li> 或尝试<li><%= link_to "Settings", edit_user_path(@user.id) %></li>这可能工作。我希望这有帮助。

+0

在sign_in或sign_up之前,我不会收到错误。只有在我登录或注册后,current_user才是nilClass。 – codigomonstruo

相关问题