簡體   English   中英

使用Ajax渲染部分集合

[英]Rendering partial collection with Ajax

我認為我的javascript工作正常,但現在無法加載內容。 只是一個空白。 我目前在我的create.js.erb中

$('#pit_form').remove(); //remove form
$('#new_link').show();  //show new link again
$('#pit_index').append("<%= j (render :partial => 'pits/pit', :collection => @pits) %>")

我也在pits_controller的create動作中添加了@pits。

調節器

class PitsController < ApplicationController
  before_action :current_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy

def new
  @pit = Pit.new
end

def index
  @pit = Pit.all
  @user = User.find_by(params[:id])
  @pits = Pit.paginate(:page => params[:page]).order('created_at DESC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end




def create
    @pit = current_user.pits.create(pit_params)
    @pits = Pit.paginate(:page => params[:page]).order('created_at DESC').group_by { |pit| pit.created_at.strftime("%B %Y") }  
      respond_to do |format|
        format.html { redirect_to @pit}
        format.js
  end
end



def show
  @pit = Pit.find(params[:id])
end

def edit
end

def update
   @pit = Pit.find(pit_params[:id])
     if @pit.update_attributes(pit_params)
       redirect_to @pit
     end
end

def destroy
  @pit = Pit.find(params[:id])
  @pit.destroy
end


def upvote
  @pit = Pit.find(params[:pit_id])
  @pit.upvote_from current_user
  redirect_to pit_path(@pit)
end

def downvote
  @pit = Pit.find(params[:pit_id])
  @pit.downvote_from current_user
  redirect_to pit_path(@pit)
end


private

def correct_user
    @pit = current_user.pits.find_by_id(params[:id])
    redirect_to root_path if @pit.nil?
  end

def pit_params
    params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end

end

我的日志說它正在加載內容,但除了空白外什么都沒有顯示。

日志

Started POST "/pits" for 127.0.0.1 at 2014-09-03 13:43:20 -0500
Processing by PitsController#create as JS
  Parameters: {"utf8"=>"✓", "pit"=>{"topic"=>"test", "author"=>"test", "summary"=>"test", "video_url"=>""}, "commit"=>"Start Pit"}
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "pits" ("author", "created_at", "summary", "topic", "updated_at", "user_id", "video_url") VALUES (?, ?, ?, ?, ?, ?, ?)  [["author", "test"], ["created_at", "2014-09-03 18:43:20.845193"], ["summary", "test"], ["topic", "test"], ["updated_at", "2014-09-03 18:43:20.845193"], ["user_id", 1], ["video_url", ""]]
   (1.2ms)  commit transaction
  Pit Load (0.3ms)  SELECT  "pits".* FROM "pits"   ORDER BY created_at DESC LIMIT 30 OFFSET 0
  Rendered pits/_pit.html.erb (0.0ms)
  Rendered pits/create.js.erb (2.2ms)
Completed 200 OK in 23ms (Views: 5.8ms | ActiveRecord: 2.3ms)

根據我的搜索,似乎在@create動作中添加@pits可以解決其他問題,但不是我的。 希望有所幫助。

我的index.html.erb

<div class = "container list-pits", id = "pit_index"> 
  <%= link_to "Add New Pit", new_pit_path, id: "new_link", remote: true, class: "btn btn-default" %>
  <br>
  <br>
  <% @pit.each do |pit| %>

  <div class = "container">
    <div class = "well"> 
       <h3 id="pit-title"><%= link_to pit.topic, pit_path(pit) %></h3>
       <p>by <%= link_to pit.author, '#' %></p>
          <br>
            <p><%= pit.summary %></p>
            <p>Replies (<%= pit.comments.count %>)</p>
          <br>

            <p>Pit Created by: <%= link_to pit.user.name, pit.user %> on <%= pit.created_at %></p>

            <%= link_to "View Pit", pit_path(pit), class: "btn btn-primary" %>
            <%= link_to "Delete Pit", pit_path(pit), remote: true, method: :delete, data: { confirm: 'Are you sure?' }  %>
      </div>
    </div>
      <% end %>
  </div>

_pit.html.erb

<

div class = "container list-pits", id = "pit_index"> 
  <%= link_to "Add New Pit", new_pit_path, id: "new_link", remote: true, class: "btn btn-default" %>
  <br>
  <br>
  <% @pit.each do |pit| %>

  <div class = "container">
    <div class = "well"> 
       <h3 id="pit-title"><%= link_to pit.topic, pit_path(pit) %></h3>
       <p>by <%= link_to pit.author, '#' %></p>
          <br>
            <p><%= pit.summary %></p>
            <p>Replies (<%= pit.comments.count %>)</p>
          <br>

            <p>Pit Created by: <%= link_to pit.user.name, pit.user %> on <%= pit.created_at %></p>

            <%= link_to "View Pit", pit_path(pit), class: "btn btn-primary" %>
            <%= link_to "Delete Pit", pit_path(pit), remote: true, method: :delete, data: { confirm: 'Are you sure?' }  %>
      </div>
    </div>
      <% end %>


 </div>

我根據一些建議在_pit部分中拋出了相同的內容。 謝謝。

采集

為了給你一些進一步的范圍內, collection了的說法partial幫手基本上取代了.each你的方面partial 重要的一點是,您需要使用as:參數記錄要調用的本地對象:

要在部分中使用自定義局部變量名稱,請在對部分的調用中指定:as選項:

<%= render partial: "product", collection: @products, as: :item %>

進行此更改后,您可以將@products集合的實例作為局部中的item局部變量進行訪問。

這意味着,如果要使用collection渲染局部變量,則需要確保它具有與as:方法相鄰的方法,以便為您提供正確的局部變量名稱。 此外,正如Ruby Racer在他的回答中概述的那樣,您應該只將collection參數用於數據收集

#app/views/pits/create.js.erb
$('#pit_index').append("<%= j (render :partial => 'pits/pit', collection: @pits, as: :pit) %>")

這將加載pit部分就好像你叫@pits.each do |pit| ,這意味着您將可以使用以下內容:

#app/views/pits/_pit.html.erb
<div class = "container list-pits", id = "pit_index">     
  <div class = "container">
    <div class = "well"> 
       <h3 id="pit-title"><%= link_to pit.topic, pit_path(pit) %></h3>
       <p>by <%= link_to pit.author, '#' %></p>
          <br>
            <p><%= pit.summary %></p>
            <p>Replies (<%= pit.comments.count %>)</p>
          <br>

            <p>Pit Created by: <%= link_to pit.user.name, pit.user %> on <%= pit.created_at %></p>

            <%= link_to "View Pit", pit_path(pit), class: "btn btn-primary" %>
            <%= link_to "Delete Pit", pit_path(pit), remote: true, method: :delete, data: { confirm: 'Are you sure?' }  %>
      </div>
    </div>
</div>

固定

您有幾個問題(我已經在上面解決了),即您在部分代碼中使用了@pit

@pit是一個實例變量,這意味着只能在控制器中設置。 如果使用局部 @instance調用局部變量,它們的作用域與@instance vars明顯不同。

查看您的日志,似乎您的partial已被調用,但是您使用@pit.each可能僅會引起空白。 我的建議解決了這個問題

您正在使用

render :partial => 'pits/pit', :collection => @pits

如果您熟悉該文檔,則應該知道這意味着您將使用局部對象,該局部對象將為該集合中的每個對象呈現一個對象,而不是一個集合。

通用(不是來自您的代碼)部分是:

_pit.html.erb:

name: <%= pit.name %>
surname: <%= pit.surname %>
<%# etcetera %>

現在,如果要使用包含集合的局部,則應該更改局部和對其的調用。

更改部分:
將每個@pits (或@pit ,更改為粘貼)更改為pits

更改調用方式(發送局部變量):
render :partial => 'pits/pit', locals: {pits: @pits}

這應該可以解決問題。

更新:

更改此:

<% @pit.each do |pit| %>

對此:

<% pits.each do |pit| %>

和這個:

$('#pit_index').append("<%= j (render :partial => 'pits/pit', :collection => @pits) %>")

對此:

$('#pit_index').append("<%= j (render :partial => 'pits/pit', locals: {pits: @pits}) %>")

更新您的@pits是數組的哈希(已使用group_by {})。 因此,您可能應該這樣更改部分內容:

<% pits.each do |current_value, pitarrray| %>
    <%# maybe do some grouping actions here %>
    <% pitarray.each do |pit| %>

別忘了,循環<% end %>時會有兩個<% end %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM