繁体   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