【发布时间】:2020-07-31 11:31:56
【问题描述】:
我的应用程序上有用户关注和取消关注按钮。我不想做任何花哨的事情,我只是不想每次点击关注或取消关注按钮时都刷新页面。
我的控制器
relationships_controller.rb
def create
current_user.follow(@user)
respond_to do |format|
format.html { #handle HTML, i.e. full page reload }
format.js # handle ajax request
end
end
def destroy
current_user.unfollow(@user)
respond_to do |format|
format.html
format.js # this one handle the request comes from `remote: true` button
end
end
我的看法
tweets/index.html.erb
<% if current_user.id != tweet.user.id %>
<% if current_user.following?(tweet.user) %>
<%= button_to "Unfollow", relationships_path(user_id: tweet.user), remote: true, method: :delete, :class => "btn btn-primary" %>
<% else %>
<%= button_to "Follow", relationships_path(user_id: tweet.user), remote: true, :class => "btn btn-primary" %>
<% end %>
<br>
<% end %>
<hr/>
<% end %>
关系模型
relationship.rb
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
用户模型
User.rb
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
def follow(user)
active_relationships.create(followed_id: user.id)
end
def unfollow(user)
active_relationships.find_by(followed_id: user.id).destroy
end
def following?(user)
following.include?(user)
end
路线
routes.rb
resource :relationships, :only => [:create, :destroy]
应用程序.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("chartkick")
require("chart.js")
//= require jquery3
//= require popper
//= require bootstrap-sprockets
检查过的按钮元素
<form class="button_to" method="post" action="/relationships?user_id=1" data-remote="true"><input class="btn btn-primary" type="submit" value="Follow"><input type="hidden" name="authenticity_token" value="hfwF8wXBcp/OM2P/pCYBnEBrjw22BDKWbw/dZFwwDsRpiIFq5jBKS/AoTMjkCZRrGum7UyW1kaL3h/4XEM2wIg=="></form>
当我点击跟随时,现在什么都没有发生。我想我的视图中需要一个新的 js 文件,但不确定如何实现它。
我已经研究过解决方案,但它们种类繁多,并且试图做的比我想做的更多,这只是简单的不刷新。
如何最好地做到这一点? (如果需要可以提供更多代码)
编辑:这段代码让 jQuery 在我的 ..webpack/enironment.js 文件中的 Rails 6 中工作
# app/config/webpack/environment.js
const {environment} = require('@rails/webpacker');
const webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery' # or if its not work specify path `'jquery/src/jquery'` which node_modules path for jquery
}));
module.exports = environment;
对于@max
$(document).on('ajax:success', '.follow-btn', function(event){
let $el = $(this);
let method = this.dataset.method;
if (method === 'post') {
$('.follow-btn[href="'+this.href+'"]').each(function(el){ $(el).text('Unfollow'); });
this.dataset.method = 'delete';
} else if (method === 'delete') {
$('.follow-btn[href="'+this.href+'"]').each(function(el){ $(el).text('Follow'); });
this.dataset.method = 'post';
}
});
【问题讨论】:
-
您是否尝试过检查浏览器中的
<button>元素以确保设置了data-remote="true"属性?另外,你能发布你的javascript清单吗?您需要使用jquery和ujs才能使远程选项生效。 -
将两者都添加到问题中。 'data-remote="true"' 似乎已设置
-
好的,现在它实际上正在工作,但我希望关注按钮更改为取消关注按钮点击(参见我上面的视图)。现在视图中没有任何反应。如果我刷新取消关注按钮,虽然这意味着关注操作有效,但我需要它只是更改为取消关注而不刷新
-
您将不得不编写一些 JavaScript 来更改按钮的状态。这超出了您原始问题的范围,但您需要为相应的控制器操作创建
.js.erb文件,并根据控制器的响应更改状态。有关系。 -
好的,谢谢,这似乎比我想象的要复杂。稍后我会问一个新问题。