如何通过jquery渲染rails表单?

时间:2018-02-11 04:47:25

标签: jquery ruby-on-rails

我正在尝试将跟随和取消关注表单呈现为rails视图

Create.js.erb(我的问题在这里,因为这是错误的代码只作用于CSS而不是rails表格,我似乎无法处理它D:)

$('.btn-danger').attr('class', 'btn')
                   .val('Following')
                   .attr('id', 'btn-unfollow');

destroy.js.erb(这也是错误的代码只作用于CSS,而不是rails表格)

$('.btn').attr('class', 'btn-danger')
                   .val('unfollowed')
                   .attr('id', 'btn-follow');

my Postsindex.html.erb

<% @posts.each do |f| %>
<%= f.headline %>
<%- f.text %> 
    <% if current_user.id != f.user.id %>
    <% if !current_user.following?(f.user) %>
    <%= form_for(current_user.active_relationships.build, remote: true) do |s| %>
      <div> <%= hidden_field_tag :followed_id, f.user.id %> </div>
      <%= s.button "follow", class: "btn btn-danger", id: "follow-btn" %>
      <% end %>
      <% else %>
      <%= form_for(current_user.active_relationships.find_by(followed_id: f.user.id), :html => {method: :delete}, remote: true) do |s| %>
      <%= s.button "unfollow", class: "btn", id: "unfollow-btn", remote: true %>
      <% end %>
      <% end %>
      <% end %>

<% end %>

发布控制器:

class PostsController < ApplicationController
 def new
    @post = Post.new
  end
def index
@posts = post.all
end
end

唯一的问题是跟随和取消关注不是局部的,而是在同一页面上,所以如果我能分别渲染它们,我会感激...

2 个答案:

答案 0 :(得分:0)

我会为你的情况做下面的事情。希望您也能发现该解决方案也很有用。

  1. 创建一个单独的控制器followersController。它将处理所有跟随/取消关注的相关活动。 (我相信你也创造了一个模型)
  2. 尝试使用控制器中的:js回复视图
  3. 因此,如果有人点击关注按钮,则会点击controller > create方法。创建后,通过呈现名为create.js.erb的js文件并编写适当的代码来更改视图。
  4. 如果有人点击取消关注按钮,则会点击controller > destroy方法。创建后,通过呈现名为destroy.js.erb的js文件并编写适当的代码来更改视图。
  5. 在视图中,尝试在follow / unfollow按钮中使用不同的id,以便在js文件中轻松处理selectors
  6. create.js.erb 文件中,您应该执行以下操作:

    $('#follow_button_parent_div').html("<%=j render partial: 'unfollow_button_html_file' %>")
    

    _unfollow_button_html_file.html.erb 中,使用取消关注按钮编写<form></form>作为提交以销毁最近创建的关注记录。

    对于销毁部分,相应地修改代码。

答案 1 :(得分:0)

我会做这样的事情。请更改以适应数据库中的模型和字段

帖子视图 我会建立两个链接来关注或取消关注。只有一个是可见的(取决于帖子的作者,如果我已经跟随他)。这些链接将使ajax帖子跟随/取消关注帖子的作者并将切换链接(隐藏自己并显示其他链接)

<% @posts.each do |post| %>
  <%= post.headline %>
  <%- post.text %> 
  <% if current_user.id != post.user.id %>
    <%= render partial: 'follow_links', locals: { user: post.user }
  <% end %>
<% end %>

部分follow_links。

<% show_follow_link   = current_user.following?(user) ? 'hidden' : '' %>
<% show_unfollow_link = current_user.following?(user) ? '' : 'hidden' %>

<!-- links to follow/unfollow have data-attributes that include the path to make the ajax post and the user to follow, that is used to find the link to show after the ajax call -->
<%= link_to 'Follow',   '#', { class: 'follow-user btn-success #{show_follow_link}', "data-url": follow_user_path(user.id), "data-followee": user.id } %>
<%= link_to 'Unfollow', '#', { class: 'unfollow-user btn-danger #{show_unfollow_link}', "data-url": unfollow_user_path(user.id), "data-followee": user.id } %>

部分

的Javascript
$('.follow-user').on("click",function() {
  target = $(this)
  url = target.attr('data-url')
  followee = target.attr('data-followee')
  other_button = $('.unfollow-user[data-followee="'+followee+'"]')

  $.ajax( {
    url: url,
    type: 'post',
    success: function() {
      target.addClass('hidden');
      other_button.removeClass('hidden');
    },
    error: function(ret) {
      alert(ret.responseJSON.error);
    }
  });

});

$('.unfollow-user').on("click",function() {
  target = $(this)
  url = target.attr('data-url')
  followee = target.attr('data-followee')
  other_button = $('.follow-user[data-followee="'+followee+'"]')

  $.ajax( {
    url: url,
    type: 'post',
    success: function() {
      target.addClass('hidden');
      other_button.removeClass('hidden');
    },
    error: function(ret) {
      alert(ret.responseJSON.error);
    }
  });

UsersController(关注和取消关注的新方法)

def follow
  if !current_user
    render json: { :error => 'You must log in' }, :status => :unprocessable_entity
    return
  end

  who_to_follow = User.find_by id: params[:id]
  if !who_to_follow
    render json: { :error => 'User not found' }, :status => :not_found
    return
  end

  # Change user_id by the field in the relationship that links to the followee
  followee = current_user.active_relationships.create(user_id: who_to_follow.id)
  render json: who_to_follow, :status => :ok
  return
end


def unfollow

  if !current_user
    render json: { :error => 'You must log in' }, :status => :unprocessable_entity
    return
  end

  who_to_unfollow = User.find_by id: params[:id]
  if !who_to_unfollow
    render json: { :error => 'User not found' }, :status => :not_found
    return
  end

  # Change user_id by the field in the relationship that links to the followee
  unfollowee = current_user.active_relationships.find_by user_id: who_to_unfollow.id

  if !unfollowee
    render json: { :error => 'You are not  following this user' }, :status => :unprocessable_entity
    return
  end

  unfollowee.destroy
  render json: who_to_unfollow, :status => :ok
  return

end

添加路线

post  "/users/:id/follow"    => 'users#follow',   :as => :follow_user
post  "/users/:id/unfollow"  => 'users#unfollow', :as => :unfollow_user