LikeMaBell
LikeMaBell

Reputation: 1609

Rails Project not Handling Request through AJAX

I have an otherwise functioning rails project where I'm trying to update some form controls to work over AJAX instead of HTML. But my controller continues to handle it as HTML instead of JS and I'm not sure why.

The link I'm trying to make remote in my users/index.html.erb:

<%= link_to 'Disable', disable_user_path(user), :confirm => 'Are you sure?',
:method => :put, :remote => true %>

Rendered as:

<a href="/users/1/disable" data-confirm="Are you sure?" data-method="put"
data-remote="true" rel="nofollow">Disable</a>

My Users Controller:

def disable
  @user = User.find(params[:id])
  if @user.update_attribute(:enabled, false)
    respond_to do |format|
      format.html { redirect_to root_path, :flash => { :success => "Disabled." }}
      format.js {}
    end
  else
    redirect_to root_path, :flash => { :error => @user.errors.full_messages }
  end
end

My includes in application.html.erb:

<%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "//ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "jquery.rails.js", "application" %>

I think I pulled down the third and fourth files from a standard place, but if those are possibly the culprit I can look inside them or track down more information about them.

When I test it, it doesn't prompt me with "Are you sure?" and it responds with the redirection and "Disabled" flash message, and the server logs confirm it's being handled as html.

Started GET "/users/1/disable" for 127.0.0.1 at 2012-02-11 17:17:31 -0200 Processing by UsersController#disable as HTML

Upvotes: 0

Views: 217

Answers (3)

LikeMaBell
LikeMaBell

Reputation: 1609

The problem ended up being that my includes weren't grabbing all the files that were apparently needed. I blasted my javascripts directory, installed the gem from scratch per the instructions in the readme, and changed my include to this:

<%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", :defaults %>

Upvotes: 0

Substantial
Substantial

Reputation: 6682

It looks like jQuery is not being loaded.

Check your include tag, specifically the resource path...

 <%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/....." %>

Most browsers will interpret //... as a local resource, as in local to the browser.

Paste the URL into a browser and see if it will load. I bet it will fallback to the file:// scheme, then complain about not being found (on your local machine).

Either add a http:// scheme on there, or get rid of the double slashes.

 <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/....." %>
   # or...
 <%= javascript_include_tag "ajax.googleapis.com/ajax/libs/....." %>

Upvotes: 0

miked
miked

Reputation: 4499

You may still need to include the unobtrusive javascript adapter if you have not done so already.

Upvotes: 2

Related Questions