我希望能够在我的控制器中渲染多个 Json 渲染。我有一个使用 Model serializers gem 的 rails Json API。目前我只能渲染一个对象。我想做的是也渲染@news 和@users,但现在我只渲染@articles。
后端文章controller.rb:
class ArticlesController < ApplicationController
impressionist :actions=>[:show]
# GET /articles
# GET /articles.json
def index
@articles = Article.where(:is_published => true).order('created_at DESC').page(params[:page]).per(6)
@news = Article.where(:news => true)
@users = User.all
respond_to do |format|
format.html # show.html.erb
format.json { render json: @articles.all }
end
end
end
后端文章序列化器:
class ArticleSerializer < ActiveModel::Serializer
attributes :id, :title, :teaser_title, :content, :teaser_content, :category, :author, :published, :num_comments, :tags, :featured, :app, :news, :tech, :device, :game, :laptop, :image, :user_id, :is_published, :created_at, :updated_at, :impressionist_count, :previous_post, :next_post, :user_id
end
前端站点文章模型:
require 'active_resource'
class Article < ActiveResource::Base
self.site = "http://localhost:3000"
end
前端站点文章控制器:
class ArticlesController < ApplicationController
def index
@articles = Article.where(:is_published => true)
@news = Article.where(:is_published => true, :news => true)
@users = User.all
end
end
前端站点文章 index.html.erb:
<div id='outer-wrapper'>
<div class='margin-1200'>
<div id='content-wrapper'>
<!--Featured Post Home-->
<div class='coverflow section' id='coverflow'>
<ul id="lightSlider">
<% @news.each do |news| %>
<% if news.is_published? %>
<li class="recent-box" style="overflow: hidden; float: left; width: 395px; height: 292px;"><div class="imageContainer"><a target="_top" href="<%= seofy_article_url(news) %>"><img alt="<%= news.title %>" title="<%= news.title %>" src="<%= api_domain_image(news) %>" class="label_thumb"></a></div>
<%= link_to news.title, seofy_article_url(news), :class => "label_title" %>
<div class="toe"><span class="post-date" href="http://socioism.blogspot.com/2015/01/the-year-2015.html"><i class="fa fa-calendar"></i> <%= article_date(news) %></span><span target="_top" href="http://socioism.blogspot.com/2015/01/the-year-2015.html#comment-form" class="recent-com"><i class="fa fa-comment"></i> 12 Comments</span></div></li>
<% else %>
No NEWS!
<% end %>
<% end %>
</ul>
</div>
<div style="clear:both;"></div>
<div class="index">
<div id='main-wrapper'>
<div class='main section' id='main'>
<div class='widget Blog' data-version='1' id='Blog1'>
<div class='blog-posts hfeed'>
<% @articles.each do |article| %>
<% if article.is_published? %>
<div class="date-outer">
<div class="date-posts">
<div class='post-outer'>
<div class='wrapfullpost'>
<div class='post hentry'>
<a name='1001054977757960770'></a>
<div class='post-header-line-1'></div>
<div class='post-body entry-content'>
<div class='post-summary' id='summary1001054977757960770'>
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: left;">
<img border="0" src="<%= api_domain_image(article) %>" />
</div>
<br />
</div>
<div>
</div>
</div>
</div>
<h2 class='post-title entry-title pagetitle'>
<a href='<%= seofy_article_url(article) %>'><%= article.title.first(40) %>...</a>
</h2>
<div class='post-details'>
<span class='post-date'><i class='fa fa-calendar'></i>
<%= article_date(article) %></span>
<span class='post-label'><i class='fa fa-tags'></i>
<%= article.tags %></span>
</div>
<div style='clear: both;'></div>
</div>
</div>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
<!-- end main wrapper -->
</div>
<!-- end content-wrapper -->
</div>
</div>
<!-- end outer-wrapper -->
</div>
</div>
出于某种原因,我设置的定义似乎没有任何影响,因为无论@news 在@articles 上是什么,即使我已经设置了我想要在哪里专门使用的东西。