【发布时间】: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:infind_one' (gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:314:infind_with_ids'(宝石) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:107:infind' (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:infind' (gem) inherited_resources-1.3.1/lib/inherited_resources/base_helpers.rb:44:inresource' (gem) cancan-1.6.9/lib/cancan/inherited_resource.rb:12:inload_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:inload_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:inrun_callbacks'(宝石) actionpack-3.2.13/lib/abstract_controller/callbacks.rb:17:inprocess_action' (gem) actionpack-3.2.13/lib/action_controller/metal/rescue.rb:29:inprocess_action'(宝石) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:30:inblock 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:ininstrument' (gem) activesupport-3.2.13/lib/active_support/notifications.rb:123:ininstrument'(宝石) actionpack-3.2.13/lib/action_controller/metal/instrumentation.rb:29:inprocess_action' (gem) actionpack-3.2.13/lib/action_controller/metal/params_wrapper.rb:207:inprocess_action'(宝石) activerecord-3.2.13/lib/active_record/railties/controller_runtime.rb:18:inprocess_action' (gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:inprocess'
(gem) actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:inprocess' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:indispatch'
(宝石) actionpack-3.2.13/lib/action_controller/metal/rack_delegation.rb:14:indispatch' (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:incall' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:73:indispatch'(宝石) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:36:incall' (gem) journey-1.0.4/lib/journey/router.rb:68:inblock in call' (gem) Journey-1.0.4/lib/journey/router.rb:56:ineach' (gem) journey-1.0.4/lib/journey/router.rb:56:incall' (gem) actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:612:incall' (gem) meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:incall'
请帮助ActiveRecord::RecordNotFound - 找不到 id= 的用户
【问题讨论】:
标签: ruby-on-rails