vendredi 3 juin 2016

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?




Aucun commentaire:

Enregistrer un commentaire