2016-11-04 126 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

相关问题