简体   繁体   English

ActiveRecord :: RecordNotFound-在Rails中找不到ID =的用户

[英]ActiveRecord::RecordNotFound - Couldn't find User with id= in Rails

I am following this tutorial link for JQuery validations. 我正在按照本教程链接进行JQuery验证。

In the link they gave the routes as: 在链接中,他们给出的路线为:

in routes.rb 在routes.rb中

map.check_email “users/check_email”, :controller => “users”, :action => “check_email”
map.resources :users

As it is throwing error, I changed: 由于它引发错误,因此我更改了:

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

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

Added jquery.validate to assets/javascripts. 已将jquery.validate添加到资产/ javascript。

users_controller.rb: 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 => !@user }
  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

In sessions/new,the login form is followed by Forgot password. 在会话/新会话中,登录表单后跟“忘记密码”。 app/views/sessions/new.html.erb: app / views / sessions / 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>

I am calling controller in the above script with remote. 我在上述脚本中使用遥控器调用控制器。 The required and email validations are fine but it is throwing error when checking the method. 必需的验证和电子邮件验证都可以,但是在检查方法时会抛出错误。 Infact it is not going to the method check_email and showing id=check_email in the log. 实际上,它不会转到方法check_email并在日志中显示id = check_email。 This is my log: 这是我的日志:

Started GET "/login" for 127.0.0.1 at 2016-11-04 15:36:22 +0530 Processing by SessionsController#new as HTML Rendered sessions/new.html.erb within layouts/home (1152.6ms) Rendered home/_header.html.erb (0.5ms) Rendered home/_footer.html.erb (0.4ms) Completed 200 OK in 1718ms (Views: 1315.0ms | ActiveRecord: 0.0ms) 在2016-11-04 15:36:22 +0530由SessionsController#new处理为HTML的GET“ / login”开始于127.0.0.1呈现为HTML呈现了layouts / home中的session / new.html.erb(1152.6ms)呈现了home / _header .html.erb(0.5ms)渲染的home / _footer.html.erb(0.4ms)在1718ms内完成200 OK(查看:1315.0ms | ActiveRecord:0.0ms)

Started GET "/assets/application.js" for 127.0.0.1 at 2016-11-04 15:36:24 +0530 RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request. 在2016-11-04 15:36:24 +0530 RailsDevTweaks:针对此请求跳过ActionDispatch :: Reloader挂钩,开始为127.0.0.1获取GET“ /assets/application.js”。

Started GET "/users/check_email?user%5Bemail%5D=test1%40gmail.com" for 127.0.0.1 at 2016-11-04 15:36:38 +0530 RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request. 在2016-11-04 15:36:38 +0530 RailsDevTweaks:跳过ActionDispatch :: Reloader挂钩以获取127.0.0.1的GET“ /users/check_email?user%5Bemail%5D=test1%40gmail.com”。 Processing by UsersController#show as JSON Parameters: {"user"=>{"email"=>"test1@gmail.com"}, "id"=>"check_email"} User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."account_id" IS NULL AND "users"."id" = ? 由UsersController#show作为JSON参数进行处理:{“ user” => {“ email” =>“ test1@gmail.com”},“ id” =>“ check_email”}用户负载(0.5ms)选择“ users”。 *在“用户”中,在“用户”中。“ account_id”为空,而“用户”。“ id” =? AND (users.deleted_at IS NULL) LIMIT 1 [["id", "check_email"]] Completed 404 Not Found in 331ms AND(users.deleted_at IS NULL)LIMIT 1 [[“ id”,“ check_email”]]已完成404在331ms内找不到

ActiveRecord::RecordNotFound - Couldn't find User with id=check_email [WHERE "users"."account_id" IS NULL AND (users.deleted_at IS NULL)]: ActiveRecord :: RecordNotFound-找不到ID = check_email的用户[WHERE“ users”。“ account_id”为NULL AND(users.deleted_at IS NULL)]:
(gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:343:in 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/relation/finder_methods.rb:107:in find' (gem) activerecord-3.2.13/lib/active_record/associations/collection_association.rb:95:in find' (gem) activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:in find' (gem) inherited_resources-1.3.1/lib/inherited_resources/base_helpers.rb:44:in resource' (gem) cancan-1.6.9/lib/cancan/inherited_resource.rb:12:in load_resource_instance' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:32:in load_resource' (gem)activerecord-3.2.13 / lib / active_record / relation / 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 / relation / finder_methods.rb:107:in find' (gem) activerecord-3.2.13/lib/active_record/associations/collection_association.rb:95:in find'(gem)activerecord -3.2.13 / lib / active_record / associations / 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:in load_and_authorize_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:10:in block in add_before_filter' (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:429:in _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:in 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/instru (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 load_and_authorize_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:10:in块(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:在_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在instrument'(gem)activesupport-3.2.13 / lib / active_support / notifications / instru menter.rb:20:in instrument' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in instrument' (gem) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:in 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:in process_action' (gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:in process' menter.rb:20:在instrument' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in仪器中(gem)actionpack-3.2.13 / lib / action_controller / metal / 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:in process' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:in dispatch' (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:in 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.rb:73:in call' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in dispatch' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:in call' (gem) journey-1.0.4/lib/journey/router.rb:68:in block in call' (gem) journey-1.0.4/lib/journey/router.rb:56:in each' (gem) journey-1.0.4/lib/journey/router.rb:56:in call' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:in call' (gem) meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in call' (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在行动块中(gem )actionpack-3.2.13 / lib / action_dispatch / routing / route_set.rb:73:in call' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:in dispatch'(gem)actionpack -3.2.13 / lib / action_dispatch / routing / route_set.rb:36: call' (gem) journey-1.0.4/lib/journey/router.rb:68:in通话中(gem)旅途call' (gem) journey-1.0.4/lib/journey/router.rb:68:in块-1.0 .4 / lib / journey / router.rb:56:在each' (gem) journey-1.0.4/lib/journey/router.rb:56:in通话中(gem)actionpack-3.2.13 / lib / action_dispatch /routing/route_set.rb:612: call' (gem) meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in通话中'

Please helpActiveRecord::RecordNotFound - Couldn't find User with id= 请helpActiveRecord :: RecordNotFound-找不到ID为=的用户

The problem is on your routes. 问题出在您的路线上。 Check what rails docs says: 检查rails文档说什么:

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. Rails路由按指定的顺序进行匹配,因此,如果您具有资源:在get'photos / poll'上方的照片,则将在get行之前对资源行的show action路由进行匹配。 To fix this, move the get line above the resources line so that it is matched first. 要解决此问题,请将get行移到资源行上方,以使其首先匹配。

When you call the URL: /users/check_email actually you're executing the users#show action. 当您调用URL: /users/check_email实际上是在执行users#show操作。

Change your check_email route to something like 将您的check_email路由更改为类似

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

and it will work. 它会工作。

ps: don't forget to update the url on your javascript. ps:别忘了更新您JavaScript上的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:ActiveRecord :: RecordNotFound-在没有ID的情况下找不到用户 - Rails: ActiveRecord::RecordNotFound - Couldn't find User without an ID 找不到ID为(ActiveRecord :: RecordNotFound)的用户 - Couldn't find User with id= (ActiveRecord::RecordNotFound) Rails 4错误:ActiveRecord :: RecordNotFound找不到&#39;id&#39;= - Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find 'id'= Rails-ActiveRecord :: RecordNotFound(找不到带有&#39;id&#39;= 27的评论): - Rails - ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=27): Rails:ActiveRecord::RecordNotFound 找不到没有 ID 的照片 - Rails: ActiveRecord::RecordNotFound Couldn't find Photo without an ID Rails 4错误:ActiveRecord :: RecordNotFound找不到&#39;id&#39; - Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find 'id' Rails 4错误:ActiveRecord :: RecordNotFound找不到id = - Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find id= Rails 4错误:ActiveRecord :: RecordNotFound /找不到&#39;id&#39;= - Rails 4 Error: ActiveRecord::RecordNotFound / Couldn't find 'id'= Rails:“找不到带有&#39;id&#39;=&#39;的电影”(ActiveRecord :: RecordNotFound) - Rails: “Couldn't find Movie with 'id'=” (ActiveRecord::RecordNotFound) ActiveRecord :: RecordNotFound —在没有ID的情况下找不到用户 - ActiveRecord::RecordNotFound — Couldn't find User without an ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM