【问题标题】:Rails portfolio item not in descending orderRails 投资组合项目未按降序排列
【发布时间】:2014-03-31 09:24:03
【问题描述】:

我在使用我添加到我的投资组合网站的新功能时遇到了一些问题。我有许多投资组合条目,现在我已经给出了类别。迁移一切正常,并且该部分运行良好。

我遇到的唯一问题是按各自类别中的降序排列组合项目。

Show.html.haml(针对个别类别)

-if @tag.portfolios.size > 0
  -@tag.portfolios.each do |portfolio|
   =link_to markdown(portfolio.content).first(72).html_safe, portfolio
  %br

 %p.text-right
  -if User.find_by_id(session[:user_id])
=link_to 'Edit', edit_portfolio_path(portfolio)
|
=link_to 'Delete', portfolio, method: :delete, data: { confirm: 'Are you sure?' }
  -else 
%br
-else
 %p Oops, I don't know what you're searching for - but it's not here!

类别控制器

def index
 @tags = Tag.all
end

def show
 @portfolios = Portfolio.all( :order => "created_at DESC" )
end

我知道这很明显,但我就是不知道是什么。

【问题讨论】:

  • 您的意思是点击类别,您应该可以看到该类别下的所有投资组合都是DESC顺序。对吗?
  • 点击类别后,您会看到相关的投资组合条目以降序排列。

标签: ruby ruby-on-rails-3 ruby-on-rails-4 haml


【解决方案1】:

您似乎已按顺序加载了@portfolios,但您遍历了@tag.portfolios。
如果您希望Tag 以有序的方式关联portfolios,请参阅here:

class Tag
  has_many :portfolios, :order => "created_at DESC" 
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2019-11-10
    • 2016-07-26
    • 1970-01-01
    • 2014-09-16
    • 2011-02-25
    相关资源
    最近更新 更多