在我的索引视图中,我将显示的新闻数量限制为 2 个最新的
但我也想在数据库中包含所有新闻的链接
在我的新闻控制器中,我有
def index
@news = News.all(:order => "created_at DESC", :limit => 2)
end
我做了另一种方法给我所有的消息
def all
@news_all = News.all(:order => "created_at DESC")
end
我应该改为限制视图中的帖子数量吗?
这是我为显示所有新闻而制作的链接
<%= link_to 'All News', all_news_path =>
和我的习惯路线
match "news/all" => "news#all", :as => "all_news"
NewsController#show
我遇到了无法找到新闻的错误id="all"
我对 ruby 很陌生,我不知道如何做到这一点:)
更新
我像 Kien 建议的那样更新了我的代码
在我的路线中使用过的集合
resources :news do
collection do
get 'all'
end
end
我的所有新闻的索引链接
<%= link_to 'All news', all_news_path, :class => 'btn btn-mini btn-success' %>
我有一个all.html.erb
看法
<% @news_all.each do |news| %>
<h2><%= news.title %></h2>
<%= news.created_at.strftime("%Y-%m-%d %H:%M") %><br />
<%= truncate(news.body, :length => 450) %><%= link_to ' meira', news %>
<%= news.author %><br />
<% end %>
昨天这很好用,但今天我从 git 中退出,现在我undefined local variable or method all_news_path
打开了索引文件中的链接..
谁能明白为什么?
路线
root / news#index
all_news_index GET /news/all(.:format) news#all
news_index GET /news(.:format) news#index
POST /news(.:format) news#create
new_news GET /news/new(.:format) news#new
edit_news GET /news/:id/edit(.:format) news#edit
news GET /news/:id(.:format) news#show
PUT /news/:id(.:format) news#update
DELETE /news/:id(.:format) news#destroy