Ruby on rails 如何使用Ajax动态地将文本或按钮从follow切换到unfollow?

Ruby on rails 如何使用Ajax动态地将文本或按钮从follow切换到unfollow?,ruby-on-rails,ruby-on-rails-4,acts-as-follower,Ruby On Rails,Ruby On Rails 4,Acts As Follower,总之,我在控制器中有一个直接调用的函数。但是,一旦它执行,我希望它更改为follow或unfollow。我正在使用Act作为跟随者宝石,在适当更改跟随或取消跟随时遇到一些问题。有人能给我指一下正确的方向吗 在我的控制器中: CreateActiveController.rb def follow @campaign = Createcampaign.find(params[:id]) if user_signed_in? && !current_user.following

总之,我在控制器中有一个直接调用的函数。但是,一旦它执行,我希望它更改为follow或unfollow。我正在使用Act作为跟随者宝石,在适当更改跟随或取消跟随时遇到一些问题。有人能给我指一下正确的方向吗

在我的控制器中:

CreateActiveController.rb

 def follow
  @campaign = Createcampaign.find(params[:id])
if user_signed_in? && !current_user.following?(@campaign)
  current_user.follow(@campaign)
  #RecommenderMailer.new_follower(@user).deliver if @user.notify_new_follower
  flash[:notice] = "You are now following #{@current_user.username}  #{@campaign.name}."
  respond_to do |format|
  format.html { redirect_to(:back) }
  format.js {render :action => "follow_button" }
  #redirect_to :back
end
else
flash[:error] = "You must <a href='/users/sign_in'>login</a> to follow #  {@campaign.name}.".html_safe
redirect_to root_url
end
end

  def unfollow
  if current_user
  @campaign = Createcampaign.find(params[:id])
  current_user.stop_following(@campaign)
   flash[:notice] = "You are no longer following #{@campaign.name}."
    #format.js {render :action => "follow" }
   redirect_to :back

   else
   flash[:error] = "You must <a href='/users/sign_in'>login</a> to unfollow    #{@campaign.name}.".html_safe
    end
    end
def follow
@campaign=Createcampaign.find(参数[:id])
如果用户已登录?&&!当前用户。以下?(@campaign)
当前用户跟踪(@campaign)
#推荐人。新追随者(@user)。如果@user发送。通知新追随者
flash[:notice]=“您现在正在跟踪#{@current_user.username}#{@campaign.name}。”
回应待办事项|格式|
format.html{redirect_to(:back)}
format.js{render:action=>“follow_button”}
#重定向到:返回
结束
其他的
flash[:error]=“您必须遵循#{@campaign.name}。”.html\u safe
将\u重定向到根\u url
结束
结束
def展开
如果当前用户
@campaign=Createcampaign.find(参数[:id])
当前用户。停止跟踪(@campaign)
flash[:notice]=“您不再关注#{@campaign.name}。”
#format.js{render:action=>“follow”}
重定向到:返回
其他的
flash[:error]=“您必须展开#{@campaign.name}。”.html\u safe
结束
结束
在我看来,我是在玩弄基于标准显示按钮的选项

<% if current_user.id == @campaignid%>
                         you own this 
                         <%end%>
                          <%else%>
                        <% if current_user.following?(@campaign) %>
                          <%= link_to "Unfollow",    unfollow_createcampaign_path(@campaign),class: "btn btn-success btn-outline btn-sm" %>
                        <%elsif%>
                        <%= link_to "follow", follow_createcampaign_path(@campaign),class: "btn btn-success btn-outline btn-sm" %>
                        <%end%>

你拥有这个

这似乎没有帮助。我看了一些Rails示例,但它们处理提交字段或表单。也许我需要改变我的方法。非常感谢您的帮助。

使用ajax提交您的链接。为此,请将“remote:true”选项添加到您的链接中,并在以后添加一个链接以引用该链接:

<%= link_to "follow", follow_createcampaign_path(@campaign), id: "follow<%= @campaign.id %>", remote:true, class: "btn btn-success btn-outline btn-sm" %>
当您执行此操作时,rails希望您在Createcampaign视图文件夹中有一个带有控制器操作名称的javascript文件(在本例中为follow.js)

在follow.js.erb文件中,您可以放置响应对象或编写javascript来替换链接的类和href。 例如,这是我为我的帖子发送到我的视图的javascript响应:

$("#like_<%= @post.id %>").removeClass('glyphicon-heart-empty').addClass('glyphicon-heart');
$("#like_<%= @post.id %>").attr('href', "<%= unlike_post_path(@micropost.id) %>");
$(“like”).removeClass('glyphicon-heart-empty').addClass('glyphicon-heart');
$(“like”)attr('href',”);

第一行,删除以前的类(空心),并用新类(充满的心)替换它。第二行,将href链接从like_post_path更改为dismission_post_path。一旦你把它分解了,它就很简单了。

谢谢你的评论,我会看看这个。。非常感谢
$("#like_<%= @post.id %>").removeClass('glyphicon-heart-empty').addClass('glyphicon-heart');
$("#like_<%= @post.id %>").attr('href', "<%= unlike_post_path(@micropost.id) %>");