FlechMe
FlechMe

Reputation: 333

Rails app : Link sharer of Facebook request JSON instead of HTML in my

I've got a Facebook sharer with this code :

<a onclick="window.open('https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href),'facebook-share-dialog','width=626,height=436');return false;">
  <i class="fa fa-facebook"></i>
  <span class="count">0</span>
</a>

But Facebook get the content of the "json" result instead of the html result !

How can i solve this ?

Upvotes: 0

Views: 158

Answers (1)

The Lazy Log
The Lazy Log

Reputation: 3574

It depends on the order of format you defined in controller. For example, here are some cases Facebook would get JSON instead of HTML

class TestController < ApplicationController
   respond_with :json, :html
   def index
      @posts = Post.all
      respond_with(@posts)
   end
end

or

class TestController < ApplicationController
   respond_with :json, :html
   def index
      @posts = Post.all
      respond_to do |format|
         format.json{ render json: @posts }
         format.html
      end
   end
end

In both cases, you just need to move :html option to be the first option and everything will be all right.

Note: You would need to force FB to fetch your page again in order to see the result because FB caches page result.

Upvotes: 2

Related Questions