2016-11-04 123 views
0

我正在關注本教程link的JQuery驗證。ActiveRecord :: RecordNotFound - 在Rails中找不到id =的用戶

在鏈接他們給了路線爲:

在routes.rb中

map.check_email 「users/check_email」, :controller => 「users」, :action => 「check_email」 
map.resources :users 

由於它拋出的錯誤,我改變了:

match '/check_email' => 'users#check_email', :as => 'check_email' 

    devise_for :users, :controllers => {:sessions => 'sessions'}, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout" } 

新增jquery.validate資產/ JavaScript的。

users_controller.rb:

def check_email 
    Rails.logger.debug "check_email..................." 
    @user = User.find_by_email(params[:user][:email]) 
    respond_to do |format| 
    format.json { render :json => [email protected] } 
    end 
end 

    def show 
    @user = User.find(params[:id]) 
    @hide_logo_section = Rails.application.config.custom.force_report_logo_accounts.include?(@user.account.name) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render :json => @user } 
    end 
    end 

在會話/新的登錄表單,然後忘記密碼。 應用程序/視圖/會話/ new.html.erb:

<%= semantic_form_for(resource_name, :url => password_path(resource_name), :remote => true, :format => :json, :html => { :id => 'password_reset' }) do |f| %> 
      <%= f.inputs do %>   
       <%= f.input :email, :label => 'Your email address', :input_html => { :id => 'user_email_field',:placeholder => "Enter your email..."}%> 
          <label id="error_explanation1" class="error errorExplanation" style="margin-top:-10px;"></label> 

      <% end %> 
      <%= f.buttons do %> 
      <%= f.commit_button :label => 'Send me that link', :button_html => {:class => 'submit button', :id => 'forgot_password_button', :disable_with => 'Wait...' }%> 
      <% end %> 

     <% end %> 

<script type="text/javascript"> 
$(document).ready(function() { 
    alert('hellllllll'); 
    $('#password_reset').validate({ 
    debug: true, 
    rules: { 
     'user[email]': {required: true, email: true, 
     remote:'/users/check_email' } 
    } 
    }); 
}); 
</script> 

我打電話與遠程上面的腳本控制器。 所需的和電子郵件驗證都很好,但檢查方法時出現錯誤。事實上,它不會去check_email方法並在日誌中顯示id = check_email。 這是我的日誌:

發起者SessionsController#新GET 「/登錄」 爲127.0.0.1,在2016年11月4日15點36分22秒0530 處理爲HTML渲染 節/ new.html .erb在layouts/home(1152.6ms)內渲染 home/_header.html.erb(0.5ms)Rendered home/_footer.html.erb(0.4ms) 在1718ms內完成200 OK(查看:1315.0ms | ActiveRecord:0.0 MS)

入門GET 「/assets/application.js」 爲127.0.0.1,在2016年11月4日15時36分24秒 0530 RailsDevTweaks:跳繩ActionDispatch :: Reloader掛鉤 爲THI請求。

開始於2016年11月4日15時36分38秒0530 RailsDevTweaks GET 「/users/check_email?user%5Bemail%5D=test1%40gmail.com」 爲 127.0.0.1:跳繩ActionDispatch :: Reloader掛鉤對於這個請求。處理 UsersController#顯示爲JSON參數: {「user」=> {「email」=>「[email protected]」},「id」=>「check_email」}用戶 加載(0.5ms)SELECT「 users「。* FROM」users「WHERE」users「。」account_id「 IS NULL AND」users「。」id「=? AND(users.deleted_at IS NULL)LIMIT 1 [[ 「ID」, 「check_email」]]已完成404在331ms

的ActiveRecord :: RecordNotFound未找到 - 找不到用戶使用id = check_email [WHERE「 「。 「用戶ACCOUNT_ID」 IS NULL AND(users.deleted_at IS NULL)〕:
(GEM) 了activerecord-3.2.13/LIB/active_record /關係/ finder_methods.rb:343:在 find_one' (gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:314:in find_with_ids'(GEM) 了activerecord-3.2.13/LIB/active_record /關係/ finder_methods.rb:107:在 find' (gem) activerecord-3.2.13/lib/active_record/associations/collection_association.rb:95:in查找」(GEM) 了activerecord-3.2.13/LIB/active_record /協會/ collection_proxy.rb:46:在 find' (gem) inherited_resources-1.3.1/lib/inherited_resources/base_helpers.rb:44:in 資源'(gem)cancan-1.6.9/lib/cancan/inherited_resource.rb:12: load_resource_instance' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:32:in load_resource'
(gem)cancan-1.6.9/lib/cancan/controller_resource。RB:25:在 load_and_authorize_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:10:in塊在 add_before_filter '(GEM) 的ActiveSupport-3.2.13/LIB/active_support/callbacks.rb:429:在 _run__917766069841795016__process_action__2108608744963441679__callbacks' (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in __run_callback'(GEM) 的ActiveSupport-3.2.13/LIB/active_support /callbacks.rb:385:in _run_process_action_callbacks' (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:81:in run_callbacks'(GEM) ActionPack的-3.2.13/LIB/abstract_controller/callbacks.rb:17:在 process_action' (gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in process_action」(GEM) ActionPack的-3.2.13/LIB/action_controller /metal/instrumentation.rb:30:in block in process_action' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in block in instrument'(gem) activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in instrument' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in 儀器 '(GEM) ActionPack的-3.2.13/LIB/action_controller /金屬/ instrumentation.rb:29:在 process_action' (gem) actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:in process_action'(GEM) 了activerecord-3.2.13/LIB/active_record/railties/controller_runtime.rb :18:在 process_action' (gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:in過程
(GEM)ActionPack的-3.2.13/LIB/abstract_controller/rendering.rb:45:在 process' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:in調度」
(GEM) ActionPack的-3.2.13/LIB/action_controller/metal/rack_delegation.rb:14: dispatch' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:246:in block in action'(gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.r B:73:在 call' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in 調度 '(GEM) ActionPack的-3.2.13/LIB/action_dispatch /路由/ route_set.rb:36:在 call' (gem) journey-1.0.4/lib/journey/router.rb:68:in塊在 呼叫'(GEM)的旅程-1.0.4/LIB /征途/ router.rb:56:在each' (gem) journey-1.0.4/lib/journey/router.rb:56:in調用 '(GEM) ActionPack的-3.2.13/lib目錄/ action_dispatch /路由/ route_set.rb:612:在 call' (gem) meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in 呼叫'

請helpActiveRecord :: RecordNotFound - 找不到用戶id =

回答

0

問題出在您的路線上。檢查什麼rails docs說:

Rails的路線在他們指定的順序是匹配的,所以如果你有一個資源:一個get「照片/民意調查」節目行動對資源的線路路徑上面的照片前,將匹配得到線。要解決這個問題,請將資源行上方的獲取行移到第一行。

當你調用的網址:/users/check_email其實你執行users#show行動。

check_email路線更改爲類似

get '/users/validation/check_email', to: 'users#check_email', as: :check_email 

,它會工作。

ps:不要忘記更新您的JavaScript的URL。

+0

我在'devise_for:users'和'resources:users'之上添加了'post'/ users/validation/check_email'=>'users#check_email',:as =>'check_email''並且我還更新了腳本中的url作爲'遠程:'users/validation/check_email''。但是我得到的錯誤爲:ActionController :: RoutingError(沒有路由匹配[GET]「/ users/validation/check_email」): –

+0

@sachingod將路由更改爲GET,而不是'POST' ...'get'/users/validation/check_email'=>'users#check_email'' ... – Victor

+0

我剛剛更新了「get」路線的答案。 – Victor

相關問題