我一直在尝试示例过去几天没有运气。 我有一张名为Article的表格,里面有子弹,粉末和calibertitle等列。我设置了一个表单,可以输入特定的细节,如子弹品牌,粉品牌,口径名称等。如何在Ruby on Rails中按标题排序和显示?
我正在使用SQLite。
如何在标题中搜索特定单词并只显示标题和相应信息(如果标题包含该单词)?
我可以列出所有的数据,但我不知道如何对它进行排序。我还想按照字母顺序来列出数据。
此外,如果我创建了另一个控制器,是否可以列出/显示新控制器下的相同列?
/views/articles/index.html.erb
<h1>Load Data</h1>
<table>
<tr>
<th>Bullet</th>
<th>Powder</th>
<th>Caliber</th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.bullet %></td>
<td><%= article.powder %></td>
<td><%= article.calibertitle %></td>
<td><%= link_to '[View]', article_path(article) %></td>
<td><%= link_to '[Edit]', edit_article_path(article) %></td>
<td><%= link_to '[Delete]', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'Back', controller: 'welcome' %>
<p>
</table>
articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
/models/article.rb
class Article < ApplicationRecord
validates :calibertitle, presence: true,
length: { minimum: 3 }
end
“过去几天我一直在尝试示例,但没有运气。”你有什么尝试? ['#where'](http://guides.rubyonrails.org/active_record_querying.html#conditions)和['#order'](http://guides.rubyonrails.org/active_record_querying.html#ordering)很好记录 – engineersmnky
我不确定你的问题的第一部分,但通过字母存储项目,你可以简单地将'@articles = Article.order(:title)'传递给你的索引动作 –