简体   繁体   English

Rails使用form_for和Ajax显示一系列对象

[英]Rails showing series of objects with form_for and Ajax

I'm trying to show a list of "Interests" that users can "follow" by clicking on a button associated with the Interest's image. 我试图显示“兴趣”列表,用户可以通过单击与兴趣图片相关的按钮来“关注”。 Upon clicking the button to follow an Interest, the Interest (and it's corresponding image) should disappear from the list. 单击关注兴趣的按钮后,兴趣(及其对应的图像)应从列表中消失。

I'm using a hide function in a create.js.erb file called upon by a Relationship controller when the follow button is clicked, but the function will only hide the first Interest in the series of Interests--NOT the Interest the user clicked on that needs to disappear. 单击“跟随”按钮时,我在由Relationship控制器调用的create.js.erb文件中使用了隐藏函数,但是该函数只会隐藏“兴趣”系列中的第一个“兴趣”,而不是用户单击的“兴趣”在那需要消失。 So there has to be a better way to set this up, but I cannot, for the life of me, figure it out. 因此,必须有一种更好的方法来进行设置,但是我无法为自己的一生解决。 Here's my current setup: 这是我当前的设置:

My Controller 我的控制器

class StaticPagesController < ApplicationController
  def home
    @user = current_user
    @city = request.location.city
    @interests = Interest.all
  end

The home page 主页

<%= render @interests %>

The partial 部分

<div id="follow_form">                      
<div class="four columns portfolio-item interests">
    <div class="our-work">
        <a class="img-overlay">
        <%= image_tag interest.image_path %>
            <div class="img-overlay-div">
            <h4><%= interest.name %></h4>
            </br>
            <h5>Placeholder
            </br>
            <%= interest.desc %></h5>
            <%= form_for(current_user.relationships.build(followed_id: 
                             interest.id), remote: true) do |f| %> 
                <div><%= f.hidden_field :followed_id %></div>
                <%= f.submit "I'm interested", class: "b-black" %>
            <% end %>
            </div>
        </a>
        <h3><a href="#"><%= interest.name %></a><span>features info</span></h3>
    </div>
   </div>
</div>

And the create.js.erb that's called upon by a Relationships controller when you click the button: 然后单击按钮时由Relationships控制器调用的create.js.erb:

$("#follow_form").hide();

UPDATE Here is the Relationships controller for additional clarification 更新这里是关系控制器,以进一步说明

def create
  @interest = Interest.find(params[:relationship][:followed_id])
  current_user.follow!(@interest)
  respond_to do |format|
      format.html { redirect_to(root_url) }
      format.js 
   end
end

You need to provide something to differentiate the different "follow_forms" that you have on the same page. 您需要提供一些东西来区分同一页面上的不同“ follow_forms”。 Otherwise, jQuery will just select the first element and only remove that one. 否则,jQuery将仅选择第一个元素,并仅删除该元素。

One idea of how to do this would be to append the "interest.id" to the end of the follow_form id 一种方法是将“ interest.id”附加到follow_form id的末尾

Update your partial to be like this (assuming your partial is called and located in /app/views/interests/_follow_form.html.erb: 将您的局部更新为这样(假设您的局部被调用并位于/app/views/interests/_follow_form.html.erb中:

<div id="follow_form<%="#{interest.id} %>">                      
<div class="four columns portfolio-item interests">
    <div class="our-work">
        <a class="img-overlay">
        <%= image_tag interest.image_path %>
            <div class="img-overlay-div">
            <h4><%= interest.name %></h4>
            </br>
            <h5>Placeholder
            </br>
            <%= interest.desc %></h5>
            <%= form_for(current_user.relationships.build(followed_id: 
                             interest.id), remote: true) do |f| %> 
                <div><%= f.hidden_field :followed_id %></div>
                <%= f.submit "I'm interested", class: "b-black" %>
            <% end %>
            </div>
        </a>
        <h3><a href="#"><%= interest.name %></a><span>features info</span></h3>
    </div>
   </div>
</div>

This way each follow_form will have a unique ID attached to it. 这样,每个follow_form都会附加一个唯一的ID。 Then you can loop through your interests and call a partial on each one like this: 然后,您可以遍历您的兴趣并像这样调用每个兴趣的部分:

home page: 主页:

<% @interests.each do |interest| %>
  <%= render 'interests/follow_form', :interest => interest %> 
<% end %>

create.js.erb create.js.erb

$("#follow_form_<%= "#{@interest.id}" %>").hide())

Your controller action can stay the same. 您的控制器动作可以保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM