我怎样才能生成查询的网址
例如http://localhost:3000/articles
我要的是,这个URL可以是负载查询
例如articles?author="author_name"
我应该在我的控制呢?
编辑:article_controller
def index
@articles = Article.all
end
路线
resources :authors
resources :articles
resources :categories
视图
<% @articles.each do |article| %>
<tr>
<td><%= article.no_urut %></td>
<td><%= article.judul_artikel %></td>
<td><%= article.konten %></td>
<td><%= article.category.nama_kategori %></td>
<td><%= article.author.author_name %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
正如雷说,如果添加:
<%= link_to articles_path(author: "name_here") %>
将生成的URL http://localhost:3000/articles?author=name_here
您可能会发现它更容易使用宝石来轻松实现由作者姓名文章搜索,但如果你只是想在你的ArticlesController手动执行此操作:
def index
if params[:author]
@author = params[:author]
@articles = Article.all.where(author: @author)
else
@articles = Article.all
end
end