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



我遵循这个JQuery验证教程链接。

在链接中,他们给出的路由如下:

在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。

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

在sessions/new中,登录表单后面跟着Forgot password。app/views/会议/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在日志中。这是我的日志:

开始GET "/login" for 127.0.0.1 at 2016-11-04 15:36:22 +0530处理由sessionscontroller# new作为HTML渲染会议/new.html。erb within layouts/home (1152.6ms/_header.html回家。erb (0.5ms)渲染home/_footer.html。女士erb (0.4)在1718ms内完成200个OK (Views: 1315.0ms | ActiveRecord: 0.0ms)

在2016-11-04开始为127.0.0.1获取"/assets/application.js15:36:24 +0530 railsdevtweak:跳过ActionDispatch::Reloader钩子

Started GET "/users/check_email?user%5Bemail%5D=test1%40gmail.com" for127.0.0.1 at 2016-11-04 15:36:38 +0530 RailsDevTweaks:跳过ActionDispatch::Reloader钩子。处理UsersController#show as JSON{"用户"=>{"电子邮件"=>"test1@gmail.com"},"id"=>"check_email"}用户加载(0.5ms)选择"users"。* FROM"users"WHERE"users"。"account_id"是NULL和"users"。"id" = ?AND (users.deleted_at IS NULL) LIMIT 1[["id", "check_email"]]完成404 Not Found in 331ms

ActiveRecord::RecordNotFound -无法找到id=check_email的用户("用户"的地方。"account_id"为空且(users.deleted_at为空)]:
(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 find' (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 resource' (gem) canan -1.6.9/lib/cancan/inherited_resource.rb:12:inload_resource_instance' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:32:in load_resource '
(gem) cancan-1.6.9/lib/康康舞/controller_resource.rb: 25:在load_and_authorize_resource' (gem) cancan-1.6.9/lib/cancan/controller_resource.rb:10:in块输入add_before_filter"(宝石)429年activesupport-3.2.13/lib/active_support/callbacks.rb::_run__917766069841795016__process_action__2108608744963441679__callbacks' (gem) activesupport-3.2.13/lib/active_support/callbacks.rb:405:in __run_callback' (gem)385年activesupport-3.2.13/lib/active_support/callbacks.rb::_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/金属/instrumentation.rb: 30:block in process_action' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in块乐器(宝石)activesupport-3.2.13/lib/active_support/通知/instrumenter.rb: 20:instrument' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:in 仪器(宝石)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/金属/rack_delegation.rb: 14:dispatch' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:246:in block in行动"(gem)actionpack-3.2.13/lib/action_dispatch/路由/route_set.rb: 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) journey-1.0.4/lib/journey/router。在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 称之为"

请帮助activerecord::RecordNotFound -无法找到id=

的用户

问题出在你的路线上。检查rails文档的内容:

Rails路由是按照指定的顺序匹配的,所以如果你在get 'photos/poll'上面有一个resources:photos,那么show动作的资源行路由将在get行之前匹配。要解决这个问题,请将get行移动到resources行上方,以便首先匹配它。

当你调用URL: /users/check_email时,实际上你正在执行users#show动作。

check_email路由更改为

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

,它会工作。

最新更新