【问题标题】:ActiveRecord::RecordNotFound - Couldn't find User with id= in RailsActiveRecord::RecordNotFound - 在 Rails 中找不到 id= 的用户
【发布时间】:2017-03-18 03:20:24
【问题描述】:

我正在关注本教程 link 进行 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.validate 添加到 assets/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 => !@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

在 session/new 中,登录表单后面是忘记密码。 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>

我在上面的脚本中用遥控器调用控制器。 所需的验证和电子邮件验证都很好,但是在检查方法时会抛出错误。事实上,它不会使用 check_email 方法并在日志中显示 id=check_email 。 这是我的日志:

在 2016-11-04 15:36:22 +0530 开始为 127.0.0.1 获取“/login” 由 SessionsController#new 处理为 HTML 呈现 layouts/home (1152.6ms) 中的 session/new.html.erb 渲染 home/_header.html.erb (0.5ms) 渲染 home/_footer.html.erb (0.4ms) 在 1718ms 内完成 200 OK (Views: 1315.0ms | ActiveRecord: 0.0ms)

于 2016 年 11 月 4 日开始为 127.0.0.1 获取“/assets/application.js” 15:36:24 +0530 RailsDevTweaks:跳过 ActionDispatch::Reloader 挂钩 为这个请求。

开始 GET "/users/check_email?user%5Bemail%5D=test1%40gmail.com" 127.0.0.1 在 2016-11-04 15:36:38 +0530 RailsDevTweaks:跳过此请求的 ActionDispatch::Reloader 挂钩。处理者 UsersController#show as JSON 参数: {"user"=>{"email"=>"test1@gmail.com"}, "id"=>"check_email"} 用户 加载 (0.5ms) SELECT "users".* FROM "users" WHERE "users"."account_id" 是 NULL 并且“用户”。“id”=? AND (users.deleted_at IS NULL) 限制 1 [["id", "check_email"]] Completed 404 Not Found in 331ms

ActiveRecord::RecordNotFound - 找不到 id=check_email 的用户 [WHERE "users"."account_id" IS NULL AND (users.deleted_at IS NULL)]:
(宝石) 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'(宝石) 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:infind'(宝石) 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:inload_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:inblock in add_before_filter'(宝石) 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'(宝石) 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'(宝石) 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'(宝石) 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:inblock 在仪器中'(宝石) 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 instrument'(宝石) 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'(宝石) 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:inprocess'
(gem) actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:in process' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:indispatch'
(宝石) 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:inblock in 行动'(宝石) 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'(宝石) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:in call' (gem) journey-1.0.4/lib/journey/router.rb:68:inblock in call' (gem) Journey-1.0.4/lib/journey/router.rb:56:in each' (gem) journey-1.0.4/lib/journey/router.rb:56:incall' (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'

请帮助ActiveRecord::RecordNotFound - 找不到 id= 的用户

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    问题出在您的路线上。查看rails docs 所说的内容:

    Rails 路由按照指定的顺序进行匹配,因此如果您在 get 'photos/poll' 上方有一个资源 :photos,则资源行的显示操作的路由将在 get 行之前匹配。要解决此问题,请将 get 行移到资源行上方,以便首先匹配。

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

    将您的 check_email 路由更改为类似

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

    它会起作用的。

    ps:不要忘记更新 javascript 上的 url。

    【讨论】:

    • 我在devise_for :usersresources :users上方添加了post '/users/validation/check_email' =&gt; 'users#check_email', :as =&gt; 'check_email',我还将脚本中的url更新为remote:'users/validation/check_email'。但我收到错误:ActionController::RoutingError (No route matches [GET] "/users/validation/check_email"):
    • @sachingod 将路由改为GET,而不是POST...get '/users/validation/check_email' =&gt; 'users#check_email'...
    • 我试过了,它没有显示错误,但它不会去控制器。我使用 puts 和 Rails.logger.debug 对其进行了测试
    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    相关资源
    最近更新 更多