为什么我的ajax不能用于link_to而是button_to?

时间:2012-12-13 11:17:25

标签: ruby-on-rails ruby-on-rails-3 jquery

我想像下面一样使用link_to,但它不起作用。它没有做任何事情:(

<% 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 %>

但如果我用button_to编码就像下面一样,那就完美了。为什么???

<% 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 %>

2 个答案:

答案 0 :(得分:1)

<% 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 %>

我搜索link_to atm:)

[编辑]

好吧,我做到了:)我需要将一些跟随动作移动到用户控制器并使用特定路由为link_to。我删除了rubycas布局并使用简单的autise设计,种子在项目中。

https://github.com/senayar/follower

答案 1 :(得分:0)

 <%= 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 %>

尝试上述,这将适用于您的引导图标