ruby on rails - redirect_to using PATCH verb not GET -


i in process of migrating of requests in rails 4 app ajax , having bit of trouble redirect.

on home page have list of items can enabled/disabled, easy enough. taken care 1 of 2 links <%= link_to "disable", toggle_my_class_path(my_class), method: "patch", remote: true %>

in applicationcontroller have before_action checks if current user logged , if not redirects them homepage log in form displayed.

def require_login   unless current_user     redirect_to root_url, :notice => "please log in"   end end 

the issue (i think) because of ajax call somehow patch verb being used instead of causing route problem. below log output call

started patch "/my_class/2/toggle" 127.0.0.1 @ 2014-04-10 16:33:52 -0500 processing myclasscontroller#toggle js   parameters: {"id"=>"2"} can't verify csrf token authenticity    (0.1ms)  select count(*) "users" redirected http://localhost:3000/ filter chain halted :require_login rendered or redirected completed 302 found in 9ms (activerecord: 1.3ms)   started patch "/" 127.0.0.1 @ 2014-04-10 16:33:56 -0500  actioncontroller::routingerror (no route matches [patch] "/"):   actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'   actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'   railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'   railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'   activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'   activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'   activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'   railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'   actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'   rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'   rack (1.5.2) lib/rack/runtime.rb:17:in `call'   activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'   rack (1.5.2) lib/rack/lock.rb:17:in `call'   actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'   railties (4.0.0) lib/rails/engine.rb:511:in `call'   railties (4.0.0) lib/rails/application.rb:97:in `call'   rack (1.5.2) lib/rack/lock.rb:17:in `call'   rack (1.5.2) lib/rack/content_length.rb:14:in `call'   rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'   /home/***/.rvm/rubies/ruby-head/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'   /home/***/.rvm/rubies/ruby-head/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'   /home/***/.rvm/rubies/ruby-head/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread' 

you can see actioncontroller::routingerror (no route matches [patch] "/"): issue

i'm not sure of way around this. don't want patch verb used can't seem find way specify verb in redirect_to

any thoughts?

you can't redirect server side (controller) within ajax call. retrieved via javascript returns javacript; web browser expecting javascript evaluated.

so need perform redirect using javascript.

for example, can change before_action (code not tested):

def require_login   unless current_user     if request.xhr?       flash[:notice] = "please log in"       flash.keep(:notice) # keep flash notice redirect       render js: "window.location = #{root_url.to_json}" # js evaluated     else       redirect_to root_url, :notice => "please log in"     end   end end 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -