【问题标题】:Rendering most recent articles rails 4渲染最近的文章 rails 4
【发布时间】:2014-06-27 18:53:26
【问题描述】:

它目前在顶部列出了我最旧的文章,我想做相反的事情。我想我需要在某个地方将它作为 created_at 订购,但我还没有让它工作。我知道这很容易,但我仍然是新手。谢谢

目前我有

<div class="bit-75">
<h2 id="title"><%= link_to article.title, article_path(article) %></h2>
<br>

<ul id="article-links">
  <div id="article-image"><%= image_tag article.image_url %></div>
  <br>
  <li id="article-text"><%= article.text %></li>
<br>
<%= article.created_at %>
<br>

<% if admin_signed_in? %>
  <li><%= link_to 'Edit',           edit_article_path(article) %></li>
  <li><%= link_to 'Destroy',        article_path(article),
                method: :delete, data: { confirm: 'Are you sure?'} %></li>
  <li><%= link_to 'New article', new_article_path %></li>
<% else %>                
  <li><%= link_to 'Make a Comment', article_path(article) %></li>              
</ul>
<% end %>   

article.rb

   class Article < ActiveRecord::Base
   has_many :comments, dependent: :destroy
   validates :title, presence: true,
                length: { minimum: 5 }
   mount_uploader :image, ImageUploader                  
   end

文章控制器

def new
 @article = Article.new
end

def index
 @article = Article.all
end

def create  
 @article = Article.new(article_params)

  if @article.save
  redirect_to @article
else
  render 'new'
 end
end

def edit
  @article = Article.find(params[:id])
end

def update
 @article = Article.find(params[:id])

 if @article.update(article_params)
   redirect_to @article
 else
   render 'edit'
 end
end


def show
  @article = Article.find(params[:id])
end

def destroy
  @article = Article.find(params[:id])
@article.destroy

 redirect_to articles_path
end

【问题讨论】:

  • 在索引操作中尝试Article.order("created_at DESC")
  • 感谢三苏。我早些时候尝试过,但我认为我犯了语法错误并且它不起作用。 default_scope 确实有效。感谢您的帮助。

标签: ruby-on-rails


【解决方案1】:

在您的文章模型article.rb 中,您可以像这样设置default_scope

default_scope -> { order('created_at DESC') }

但是,这种方法会对所有页面上的文章进行这样的排序。如果您想在一个操作中对它们进行这样的排序,比如def index,那么这样的方法可能会更好。

@articles = Article.order('created_at DESC')

就像@ShamsulHaque 在他的评论中所说的那样。

这是一篇关于default scopes 的好文章。

更新

如果你喜欢使用scopes,就像@rich 说的那样,那么语法应该是这样的:

scope :recent, ->(order = 'desc') { order(created_at: order.to_sym) }

您可以选择在您的控制器中调用ascdesc,如下所示:

@articles = Article.recent('asc')
@articles = Article.recent('desc') # although we defaulted to 'desc', so really only need Article.recent

为了解释一下,@rich 包含了to_sym 以将字符串'desc''asc' 转换为:desc:asc 之类的符号。如果你不这样做,你会得到一个类似的错误

Direction should be :asc or :desc

希望这会有所帮助。

【讨论】:

  • 这对贾斯汀很有用。谢谢,我很感激帮助。并感谢您的链接。我去看看。
  • 作为替代语法,您也可以使用order(created_at: :desc)
【解决方案2】:

Scopes

使用default_scopelittle taboo(可能会导致问题)- 使用带有条件的标准范围会更好:

#app/models/article.rb
Class Article < ActiveRecord::Base
   scope :recent, (order = "desc") -> { where created_at: order.to_sym}
end

这将允许您调用:

@article = Article.recent("asc")

@justin 答案的一个很好的扩展;)

【讨论】:

  • 感谢 Rich 的输入。
  • 真@rich,但应该是scope :recent, -&gt;(order = 'desc') { order(created_at: order.to_sym) } :)
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 2017-05-07
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
相关资源
最近更新 更多