John
John

Reputation: 13715

rails before filter redirect not manifesting

I am posting a comment with some javascript. This calls the create action in the comments controller, which has a before filter that checks that the user belongs to the correct group. The before filter has a redirect to a dashboard page, but when I attempt to post a comment with a user who is not in the correct group, the comment does not get created, but the redirect does not happen.

Here is the before filter:

  def require_correct_user
    activity = Activity.find(params[:activity_id])
    unless (current_user.group == activity.group)
      redirect_to dashboard_path
    end
  end

I see the following in the server logs:

Started GET "/dashboard" for 127.0.0.1 at 2012-02-23 14:40:07 -0500
  Processing by UsersController#dashboard as */*

But the page stays the same in the browser and does not appear to be redirecting.

What is keeping the page from redirecting? Does it have to do with the comment being posted with javascript?

CLARIFICATION:

I am posting the comment to the create action with a form:

=form_tag({ :action => 'create', :controller => 'comments', :method => 'post' }

But I am submitting the form using the jquery-form plugin and calling ajaxForm.

Upvotes: 0

Views: 354

Answers (1)

Dave Isaacs
Dave Isaacs

Reputation: 4539

If you are using ajaxForm, then you are making an ajax request via XMLHttpRequest, which mean the browser window will not refresh. All you are doing, then, is redirecting the ajax request. For this to work as you would like, you need to return a status code to the ajax request that, when processed by the client browser causes the browser to load the alternate URL.

Upvotes: 2

Related Questions