I am following this tutorial link for JQuery validations.
In the link they gave the routes as:
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.
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:
<%= 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. 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)
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.
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. 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" = ? AND (users.deleted_at IS NULL) LIMIT 1 [["id", "check_email"]] Completed 404 Not Found in 331ms
ActiveRecord::RecordNotFound - Couldn't find User with id=check_email [WHERE "users"."account_id" IS NULL AND (users.deleted_at IS NULL)]:
(gem) 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:in
find_with_ids' (gem) 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:in
find' (gem) 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:in
resource' (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:in
load_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: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:inprocess_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:inblock 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 menter.rb:20:ininstrument' (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:inprocess_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:inprocess_action' (gem) actionpack-3.2.13/lib/abstract_controller/base.rb:121:in
process'
(gem) actionpack-3.2.13/lib/abstract_controller/rendering.rb:45:inprocess' (gem) actionpack-3.2.13/lib/action_controller/metal.rb:203:in
dispatch'
(gem) actionpack-3.2.13/lib/action_controller/metal/rack_delegation.rb:14:indispatch' (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:incall' (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:incall' (gem) journey-1.0.4/lib/journey/router.rb:68:in
block in call' (gem) journey-1.0.4/lib/journey/router.rb:56:ineach' (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:incall' (gem) meta_request-0.2.1/lib/meta_request/middlewares/app_request_handler.rb:11:in
call'
Please helpActiveRecord::RecordNotFound - Couldn't find User with id=
The problem is on your routes. Check what rails docs says:
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. To fix this, move the get line above the resources line so that it is matched first.
When you call the URL: /users/check_email
actually you're executing the users#show
action.
Change your check_email
route to something like
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.