HUSTEN
HUSTEN

Reputation: 5197

Why my ajax doesn't work with link_to but button_to?

I want to use link_to just like below but it doesn't work. It doesn't do any thing:(

<% unless user == current_user %>
  <% if current_user.following?(user) %>
    <%= link_to sanitize('<i class="icon-remove icon-white"></i> ') + 'Un-Follow', user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, :class => 'btn' %>             
  <% else %>
    <%= link_to sanitize('<i class="icon-ok icon-white"></i> ') + 'Follow', user_follows_path(user.to_param), :remote => true, :class => 'btn btn-primary' %>             
  <% end %>
<% end %>

But if I code it with button_to just like below, it works perfectly. Why???

<% unless user == current_user %>
  <% if current_user.following?(user) %>
    <%= button_to("Un-Follow #{user.username}", user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true) %>
  <% else %>
    <%= button_to("Follow #{user.username}", user_follows_path(user.to_param), :remote => true) %>
  <% end %>
<% end %>

Upvotes: 0

Views: 307

Answers (2)

rbinsztock
rbinsztock

Reputation: 3195

<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= button_to("Un-Follow #{user.username}", user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, 
:class => 'btn btn-primary') %>
<% else %>
<%= button_to("Follow #{user.username}", user_follows_path(user.to_param), 
:remote => true, :class => 'btn btn-primary') %>
<% end %>
<% end %>

i search for link_to atm :)

[edited]

ok i did it :) i need to move some follows action into user controller and use specific route for both link_to. I remove the rubycas layout and use simple devise for auth, the seed is inside the project.

https://github.com/senayar/follower

Upvotes: 1

VenkatK
VenkatK

Reputation: 1305

 <%= link_to(user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, :class => 'btn') do %>
   <i class="icon-remove icon-white"></i>
   'Un-Follow'
 <% end %>

Try above, that will work with your bootstrap icon

Upvotes: 0

Related Questions