2016-11-04 19 views
0

このチュートリアルの次の記事は、linkのJQuery検証です。リンクで ActiveRecord :: RecordNotFound - id =がRailsのユーザーを見つけることができませんでした。

は彼らが通りのルートを与えた:

で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/javascripts

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を表示していません。 これは私のログです:HTMLは セッションをレンダリングとして

が新しいSessionsController#で2016年11月4日15時36分22秒0530 処理で127.0.0.1のための "/ログインを" GET開始/ new.htmlホーム/ _header.html.erb(0.5ms)レンダリングされたホーム/ _footer.html.erb(0.4ms) 1718msで200完了OK(閲覧:1315.0ms | ActiveRecord:0.0)スキップActionDispatch ::リロード機能フック をTHIのために:MS)は

は、2016年11月4日午後3時36分24秒 0530 RailsDevTweaksで127.0.0.1のための "/assets/application.js" をGET開始しましたsの要求。スキップActionDispatch ::リロード機能フック:

は、2016年11月4日午前15時36分38秒0530 RailsDevTweaksで 127.0.0.1のための "https://stackoverflow.com/users/check_email?user%5Bemail%5D=test1%40gmail.com" をGET開始しましたこの要求に対して処理: UsersController#show JSONパラメータ: {"user" => {"email" => "[email protected]"}、 "id" => "check_email"}ユーザ ロード(0.5ms)SELECT " * "FROM" users "WHERE" users "。" account_id " IS NULLと" users "。" id "=?そして、 は[[ "ID"、 "check_email"]] 331ms

のActiveRecord :: RecordNotFoundには見られない404を完了LIMIT 1(users.deleted_atがNULL IS) - ID = check_email [ "を持つユーザーが見つかりませんでした」。 "のユーザーACCOUNT_ID" はNULLで、(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:46: で find' (gem) activerecord-3.2.13/lib/active_record/associations/collection_association.rb:95:inのfind」(宝石) のActiveRecord-3.2.13/libに/ active_record /団体/ collection_proxy.rbで(gem)cancan-1.6.9/lib/cancan/controller_resource。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の' IN(GEM) activesupportの-3.2.13/LIB/active_support (GEM) actionpack-3.2.13/LIB/action_controller _run_process_action_callbacks' (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:81:in run_callbacks':17 process_action' (gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:in process_actionで(GEM) actionpack-3.2.13/LIB/abstract_controller/callbacks.rb」/callbacks.rb:385:in /metal/instrumentation.rb:30:in block in process_action' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:inブロック in instrument '(gem) activesupport-3.2.13/lib/active_support/instifications/instrumenter.rb:20: 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:
(GEM) actionpack-3.2.13/LIB/action_controller/ process_action' (gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:inプロセス:45 process' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:inディスパッチで
(GEM)actionpack-3.2.13/LIB/abstract_controller/rendering.rb」でmetal/rack_delegation.rb:14: dispatch' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:246:inブロック アクション '(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)旅-1.0.4/LIB/':36(GEM) actionpack-3.2.13/LIB/action_dispatch /ルーティング/ route_set.rb call' (gem) journey-1.0.4/lib/journey/router.rb:68:inブロック内 コールで'旅/ router.rb:56:each' (gem) journey-1.0.4/lib/journey/router.rb:56:inコールで

くださいhelpActiveRecord :: RecordNotFound ':612(GEM) actionpack-3.2.13/libに/ action_dispatch /ルーティング/ route_set.rb call' (gem) meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in コールで' - ID =

のユーザーを見つけることができませんでした

答えて

0

あなたのルートに問題があります。チェックrails docsは言う:

あなたはリソースを持っているので、もしルートは、彼らは指定された順序に一致している

レール:前にマッチしますGET '写真/世論調査のリソースラインのためのshowアクションのルート上の写真をget行。これを修正するには、取得行をリソース行の上に移動して、最初に一致させます。

実際には/users/check_emailというURLを呼び出すと、users#showアクションが実行されています。

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

ような何かにあなたのcheck_emailのルートを変更し

、それが動作します。

ps:javascriptでURLを更新するのを忘れないでください。

+0

'devise_for:users'と' resources:users'の上に 'post '/ users/validation/check_email' => 'users#check_email'、:as => 'check_email'を追加してスクリプトのURLも更新しました'remote: 'users/validation/check_email''とします。しかし、私は次のようにエラーを出しています:ActionController :: RoutingError([GET] "/ users/validation/check_email"に一致するルートはありません): –

+0

@sachingod 'POST'ではなく' GET'へのルートを変更します。 'get '/users/validation/check_email '=>' users#check_email'' ... – Victor

+0

"get"ルートで回答を更新しました。 – Victor

関連する問題