我发现很多人遇到了同样的问题,但没有一个解决方案适用于我的情况。我关注Michael Hartl's tutorial here。我有一个按照第14章设置的跟随系统(我使用本书的最新版本,因为我在Rails 5.1上)。数据库中的follow / unfollow按钮进程,但我必须手动刷新页面才能看到按钮并按照计数更改。
我在浏览器控制台日志中得到了这个:
public boolean collidedBottom()
{
if(c.getBall().getBallY() <= enemy.y + enemy.height &&
c.getBall().getBallX() + c.getBall().getHitbox().width >= enemy.x
&& c.getBall().getBallX() <= enemy.x + enemy.width)
return true;
return false;
}
public boolean collidedTop()
{
if(c.getBall().getBallY() + c.getBall().getHitbox().height >= enemy.y &&
c.getBall().getBallX() + c.getBall().getHitbox().width >= enemy.x
&& c.getBall().getBallX() <= enemy.x + enemy.width)
return true;
return false;
}
relationships_controller.rb:
POST 500 (Internal Server Error)
jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:10255
send @ jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:10255
ajax @ jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:9739
ajax @ jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js:94
handleRemote @ jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js:179
(anonymous) @ jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js:512
dispatch @ jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:5227
elemData.handle @ jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js:4879
视图/用户/ _follow.html.erb:
class RelationshipsController < ApplicationController
before_action :authenticate_user!
def create
user = User.find(params[:followed_id])
current_user.follow(user)
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
end
视图/用户/ _unfollow.html.erb
<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
视图/用户/ _follow_form.html.erb
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
关系/ create.js.erb
<% if current_user != @user %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
关系/ destroy.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
$("#followers").html('<%= @user.followers.count %>');
users_controller.rb
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>");
$("#followers").html('<%= @user.followers.count %>');
我做错了什么?
答案 0 :(得分:1)
您设置了本地变量user
def create
user = User.find(params[:followed_id])
但是@user
模板中的引用类实例变量.js.erb
。
$("#followers").html('<%= @user.followers.count %>');
相反,请设置@user = User.find(params[:followed_id])
以使其可供视图使用。实际上,我认为你正试图设置@user = current_user
。这可能更有意义。
答案 1 :(得分:-1)
您需要重命名Javascript文件才能使.erb
结束。
relationships/create.js.erb
和relationships/destroy.js.erb
。这是因为你在文件中有Ruby,因此在它被解释为Javascript并发回之前它必须处理Ruby。
http://guides.rubyonrails.org/working_with_javascript_in_rails.html#a-simple-example