0

I'm implementing Ajax into my existing Rails 3.1 app. I'm comparing with Samnang Chhun's excellent tutorial - his works, mine doesn't.

This is my link_to:

<%= link_to('Do some Ajax!', @activity, :remote => true) %>

This my controller:

class ActivitiesController < ApplicationController

  respond_to :html, :js

  # ...

  def show
    @activity = Activity.find(params[:id])
    respond_with @activity
  end

  # ...

end

My views/activities folder contains both a show.html.erb and a show.js.erb.

UPDATE: This is my included JavaScript files:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/activities.js?body=1" type="text/javascript"></script>
<script src="/assets/add2home.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery.dropdownPlain.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

When I click it, the page refreshes, a straight HTML request, which is also verified by the server output (see line 2):

Started GET "/activities/18-leka-titta-pa-mig" for 127.0.0.1 at 2013-03-13 20:58:43 +0100
  Processing by ActivitiesController#show as HTML
  Parameters: {"id"=>"18-leka-titta-pa-mig"}
  Activity Load (0.4ms)  SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT 1  [["id", "18-leka-titta-pa-mig"]]
WEBSITE: Babycenter.com
  Tag Load (0.8ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 18 AND "taggings"."taggable_type" = 'Activity'
Rendered layouts/_footer.html.erb (0.6ms)
Rendered activities/show.html.erb within layouts/application (63.4ms)
Rendered layouts/_facebook_sdk.html.erb (0.1ms)
Rendered activities/_filter_tabs.html.erb (3.1ms)
Completed 200 OK in 107ms (Views: 79.8ms | ActiveRecord: 21.6ms)

What am I doing wrong?

4

2 回答 2

0

You're looking for the jquery UJS hooks - https://github.com/rails/jquery-ujs

于 2013-03-13T21:20:45.180 回答
0

I found the problem by coincidence. I turned out I used an old JavaScript snippet to disable iOS Safari to open links in new windows. This script wasn't compatible with the Rails 3 way of doing Ajax.

By replacing the script with the latest from iPhone Safari Web App opens links in new window I got it to work.

于 2013-03-16T12:48:49.710 回答