簡體   English   中英

Link_to和Ajax請求Rails

[英]Link_to and Ajax request Rails

希望我能解釋這一點,以便人們能夠理解我正在努力實現的目標。 目前,我正在創建多個帖子,這些帖子顯示在此URL上以供公眾查看

/tynewyddnews

與標准相反

/posts/:id

僅對管理員用戶可用

該頁面分為2個部分,即占據大部分頁面的最新帖子和頁面左側下方列出的歷史帖子,每個帖子在范圍上略有不同

范圍

scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}, :order => "posts.published_on DESC"

最新的帖子

@tynewyddpostlatest = Post.tynewydd_posts.first

歷史職位

tynewyddpost = Post.tynewydd_posts.reverse
tynewyddpost.pop
@tynewyddpost = tynewyddpost

在我看來,我每個人都這樣渲染

左手邊

<% @tynewyddpost.reverse.each do |t| %>
<%= link_to t.title, t %>

主要

<%= @tynewyddpostlatest.title %>

我想要實現的是,當單擊左側帖子的鏈接時,它將通過ajax代替中心的主要帖子。 到目前為止,我提出的內容可以正常工作,通過ajax調用可以顯示標題和評論,但是我單擊的帖子的照片並未在視圖中呈現,即使在螢火蟲中它們顯示為已呈現(盡管它們的大小為0x0 px)

左側職位

<% @tynewyddpost.reverse.each do |t| %>
<%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews'), :remote => true %>
<% end %>

tynewyddnews.js.erb

<% if params[:type] == 'tynewyddnews' %>
 $('.post-item').html('<%= escape_javascript(render partial: 'tynewyddnewspost') %>');
<% end %>

部分的

<div class="post-item">
  <% @tynewyddpost.each do |t| %>
   <h2><%= t.title %></h2>
    <div id="work-samples"> 
     <% for photo in t.photos %>
      <%= image_tag(photo.avatar.url(:news_images), :class => 'work-sample') %>
     <% end %>
   </div>  
  <p class="post-description"><%= t.comments.html_safe %></p><a class="post-more" href="#">Continue Reading&nbsp;&raquo;</a>
   <div class="post-item-panel">
      <ul>
       <li class="date">
        <p><%= date_output(t.published_on) %></p>
       </li>
      </ul>
  </div>
</div>
  <% end %>

非常感謝

您可以先刪除此行:

<%= @tynewyddpostlatest.title %>

並嘗試繼續顯示錯誤。 如果可以,您能否刪除:

<% @tynewyddpost.reverse.each do |t| %>
<%= link_to t.title, t %>

錯誤仍然顯示嗎?

暫無
暫無

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

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