我有一个部分(叫做:voting_post)在广播时被执行,但是它中的ruby逻辑没有被评估。这是部分:
<% if current_user && (current_user.id != post.user_id) %>
<% if current_user.voted_on?(post) %>
Voted
<% else %>
<a href="#" class="vote-up" data-post-id="<%= post.id %>">Vote</a>
<% end %>
<% end %>
基本上第一个&#39; if&#39;声明是这样的,除了帖子的作者,所有登录的人都可以投票。 第二个&#39;如果&#39;声明检查用户是否已经投票。
终端日志显示:
Rendered posts/_voting_post.html.erb (50.9ms)
[ActionCable] Broadcasting to posts: {:action=>"voteUp", :data=>”
Voted"}
PostChannel transmitting {"action"=>"voteUp", "data"=>"
Voted"} (via streamed from posts)
因此,无论用户场景如何,由广播更新的结果视图始终显示&#34; Voted&#34;即使用户没有投票。任何帮助解决这个问题都将受到赞赏。
更新
class User < ApplicationRecord
has_many :posts, dependent: :destroy
acts_as_voter
...
end
class Post < ApplicationRecord
belongs_to :user
acts_as_voteable
end
更新2: 在我的PostChannel中,我有以下内容:
def vote_up(params)
post = Post.find(params['id'])
if post.user_id != current_user.id
current_user.vote_for(post)
ActionCable.server.broadcast 'posts', action: 'voteUp', data: render_message(post)
end
end
private
def render_message(post)
ApplicationController.render(
partial: 'posts/voting_post',
locals: { post: post, current_user: current_user }
)
end