使用ajax在rails中渲染跟随/取消跟随按钮 [英] render follow/unfollow button in rails with ajax

查看:59
本文介绍了使用ajax在rails中渲染跟随/取消跟随按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了关注/取消关注功能,并且想向其中添加AJAX调用,但是我遇到了麻烦. 我的用于关注/取消关注的_follow_button.html.erb部分显示在用户"->索引"上,如下所示:

I have implemented follow/unfollow functionality and would like to add AJAX call to it, but I am stuck. My partial _follow_button.html.erb for follow/unfollow which is rendered on Users->index, looks like:

<% if current_user.id != user.id %>    

    <% if !current_user.following?(user) %>
        <%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
            <div><%= hidden_field_tag :followed_id, user.id %></div>
            <span class="follow"><%= f.submit "Follow User", class: "btn btn-primary btn-sm" %></span>
        <% end %>
    <% else %>
        <%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
                        html: { method: :delete }, remote: true) do |f| %>
            <span class="unfollow"><%= f.submit "Unfollow User", class: "btn btn-secondary btn-sm" %></span>
        <% end %>
    <% end %>

<% end %>

然后我的关系控制器看起来像:

Then my controller for relationships looks like:

class RelationshipsController < ApplicationController

  respond_to :js, :json, :html
  def create
        user = User.find(params[:followed_id])
       @follow = current_user.follow(user)
  end

  def destroy
        user = Relationship.find(params[:id]).followed 
        @unfollow = current_user.unfollow(user)
  end

end

我对用户个人资料的看法如下:

My view on user profile looks like:

<div class="col-5" style="margin-left: -5px;">
    <%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
</div>

我的route.rb定义了以下路由:

My routes.rb have the following routes defined:

resources :users do
    member do
      get :following, :followers
    end
  end

  resources :relationships, only: [:create, :destroy]

我的视图"文件夹结构包含子文件夹用户和关系".他们两个都有单独的控制器,我尝试添加简单的警报功能'alert("Works");'到这两个子文件夹中的create.js.erb尝试将它们与控制器匹配,但似乎没有一个起作用.这是我的第一个Rails项目,但我不太了解这个问题可能是什么.有什么建议吗?

My Views folder structure has subfolders Users and Relationships. Both of them have separate controllers, and I have tried adding simple alert function 'alert("Works");' to the create.js.erb in both of those subfolders to try and match them with the controller, but none don't seem to work. This is my first Rails project, and I do not quite understand what the issue could be. Any suggestions?

推荐答案


建议

有关您的上一个的建议问题:我建议-而不是在StackOverflow上发布有关调试代码的问题-为自己创建一个良好的调试环境.


An advice

An advice, also regarding your last question: I would recommend - instead of posting questions about debugging code on StackOverflow - create a good debugging environment for yourself.

Beebug 使用JavaScript ! -它确实帮助我掌握了要点,并了解了Rails和ajax的数据流.

Byebug or Binding pry is a good place to start, but before you can use those properly you need to understand the code you are using. I would recommend reading Working with Javascript in depth! - it really helped me getting the hang of it and understanding the dataflow of Rails and ajax.

我认为,这将打破我本人长期困扰的牢不可破的Stackoverflow循环:

This would, i think, break the unbreakable Stackoverflow-loop, that i myself were tied to for a long time:

loop do
  puts "Try code"
  sleep 1000
  puts "Arrhh! an error!"
  sleep 1000
  puts "Posting on Stackoverflow"
  sleep 1000
  puts "Waiting for answer"
  sleep 1000
end 

希望您能弄清楚!

这篇关于使用ajax在rails中渲染跟随/取消跟随按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆