Prateek Darmwal
Prateek Darmwal

Reputation: 161

link_to tag not responding to javascript after setting remote option as true in rails 4

I am trying to call a JavaScript from my link_to tag using (remote: true) option but when I click on my link it is processed as HTML not as js. Can anyone tell me what I am doing wrong. I am new to rails. Here is my code->

my partial view social_media/_fb_like.erb in which link is given:

<div style="display: inline-block;position: relative;">
    <% if post['likes']['summary']['has_liked']%>
      <img src="\assets\liked.png" width="18px" height="18px"/>&nbsp;<%=link_to "Like", social_media_fb_like_path(:pid => "#{post['id']}"),id: "to_unlike", :remote => true%>
    <%else%>
      <img src="\assets\unliked.png" width="18px" height="18px"/>&nbsp;<%=link_to "Like",social_media_fb_unlike_path(:pid => "#{post['id']}"),id: "to_like", :remote => true%>
    <%end%>
</div>

my controllers methods->

SocialMediaController#fb_like

def fb_like
  user = User.find_by_id(session[:user_id])
  @post_id = Koala::Facebook::API.new(user.token)
  postid=params[:pid]
  @post_id.put_like(postid)
end

SocialMediaController#fb_unlike

def fb_unlike
  user = User.find_by_id(session[:user_id])
  @post_id = Koala::Facebook::API.new(user.token)
  postid=params[:pid]
  @post_id.delete_like(postid)
end

and my routes in routes.rb ->

  get 'social_media/fb_like'

  get 'social_media/fb_unlike'

When i click on any link like/unlike my server console gives me this message:

Started GET "/social_media/fb_unlike?pid=1665814087069318_1675050226145704" for 127.0.0.1 at 2016-10-18 15:05:13 +0530
Processing by SocialMediaController#fb_unlike as HTML
Parameters: {"pid"=>"1665814087069318_1675050226145704"}
User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 9 LIMIT 1
Completed 500 Internal Server Error in 5256ms (ActiveRecord: 0.4ms)

What i learned is that if i set the option remote as true in link_to than it should be processing as js because of jquery_ujs, not as HTML as shown above. Please help me out. Thanks in advance

Upvotes: 1

Views: 102

Answers (1)

Prateek Darmwal
Prateek Darmwal

Reputation: 161

The answer I figured out for my question was the wrong order of the files in application.js manifest

The right order should be

//= require jquery
//= require jquery.turbolinks // if u use the gem jquery.turbolinks
//= require jquery_ujs
// ...Other js files
//= require turbolinks

Upvotes: 1

Related Questions