ruby Rails中的HTML控制器处理

gtlvzcf8  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(103)

我在profile/follow_button设置了一个turbo_frame_tag,如下所示

<%= turbo_frame_tag "#{dom_id(@user)} follow_user" do %>
  <% if following?(user) %>
    <%= link_to "Unfollow", profile_unfollow_path(id: user.id), method: :delete, class: "" %>
  <% else %>
    <%= link_to "Follow", profile_follow_path(id: user.id), method: :post, class: "" %>
  <% end %>
<% end %>

字符串
我想hotwire加载action取消跟随,跟随时,点击这个,所以我在我的控制器设置这个

def follow
    Relationship.create_or_find_by(follower_id: current_user.id, followee_id: @user.id)
    # redirect_back(fallback_location: root_path)
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: update_action_follow
      end
    end
  end

  def unfollow
    current_user.followed_user.where(follower_id: current_user.id, followee_id: @user.id).destroy_all
    # redirect_back(fallback_location: root_path)
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: update_action_follow
      end
    end
  end

  private
  def update_action_follow
    private_target = "#{dom_id_for_follower(@user)} follow_user"
    turbo_stream.replace(private_target,
                         partial: 'profile/follow_button',
                         locals: {
                           user: @user
                         })
  end

  def update_count_follower
    private_target = "#{@user.id}-follower-count"
    turbo_stream.update(private_target,
                        partial: 'profile/follower_count',
                        locals: { user: @user
                        })
  end


通常格式是TURBO_STREAM,但当我检查日志时,它是HTMLx 1c 0d1x
所以我得到的问题像

这个怎么办?

rekjcdws

rekjcdws1#

我自己得到的解决方案,只是具体的turbo_method在link_to喜欢

<% if following?(user) %>
    <%= link_to "Unfollow", profile_unfollow_path(id: user.id), class: "",data: { turbo_method: :delete } %>
  <% else %>
    <%= link_to "Follow", profile_follow_path(id: user.id), class: "",data: { turbo_method: :post }%>
  <% end %>

字符串
这是必要的,因为我正在使用Rails 7和Turbo。

相关问题