简体   繁体   中英

Redirect_to not working as expected

I'm creating a checkbox with Ajax call to update an attribute... everything is working fine but my redirects aren't working as intended (nor flash[:notices])

If i use chrome Network i can see the GET method for the desired page but no redirect whatsoever and flash[:notices] don't work (I'm assuming it's related)

So Here's my code:

Controller :

def toggle
  @prato = Prato.find(params[:id])

if params[:sugestao] 
  @sugestao = params[:sugestao]
  if @sugestao == "true" and Prato.where("sugestao = ? AND categoria_pratos_id = ?", "1" , @prato.categoria_pratos_id).count <= 2
 @prato.update_attributes(:sugestao => @sugestao)

else

if @sugestao == "true" and Prato.where("sugestao = ? AND categoria_pratos_id = ?", "1" , @prato.categoria_pratos_id).count > 2
  flash[:notice] = "nao"

end
end
if @sugestao == "false"
   @prato.update_attributes(:sugestao => @sugestao)

end
end


if params[:menu]
  if @prato.update_attributes(:menu => params[:menu])


  else

  end
else 
  if params[:ativo]
    if @prato.update_attributes(:ativo => params[:ativo])

  else

  end
end
end
flash[:notice] = "fds"
redirect_to "/pratos/new?"
end

View:

<%= check_box_tag 'Ativo', prato.id , prato.ativo,:class => "task-check2", :id=>"task-check2"  %>
<%= check_box_tag 'Sugestão', prato.id , prato.sugestao,:class => "task-check3", :id => "task-check3"  %>

Javascript

$(".task-check2").bind('change', function(){

    $.ajax({
      url: '/pratos/'+this.value+'/toggle',
      type: 'POST',
      data: {"ativo": this.checked}
    });


});


$(".task-check3").bind('change', function(){

    $.ajax({
      url: '/pratos/'+this.value+'/toggle',
      type: 'POST',
      data: {"sugestao": this.checked}
    });


});

Routes

resources :pratos do
  member do
    post 'toggle'
  end
end

Ok So everything works as intended... Problem is i want to show a message when user activates 3 checkboxes but Flash Notices and redirects aren't working... Can someone explain to me why?

You are sending the js request to controller that's why this is not redirecting and not show your flash message because your page not loaded. For this you have to change your div data using JavaScript on the success of AJAX or toggle.js.erb

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM