0
我只为我的项目使用了静态默认范围,而我最近创建了一个动态范围功能,用户可以在其中单击按钮或使用下拉菜单改变他们查看列表的顺序。允许用户更改示波器的顺序
到目前为止,我遇到了一些方法,如使用unscoping和设置一个新的范围,并看到重新排序。我正在查看文档,但我也不确定如何让用户能够选择。我会在相应的HTML.erb文件中使用类似Link_to或button_to的内容吗?
我post.rb
文件
它会看起来像这样
default_scope { order(created_at: :desc) }
scope :ordered_by_title, -> { reorder(title: :asc) }
scope :ordered_by_reverse_created_at, -> { reorder(created_at: :ASC)}
我添加这些其他范围内的默认下,因为我认为人会设定他们想要的范围和视图会叫/激活它们一旦用户点击或从呈现的页面中选择它。
在我post_controller.rb
def index
@posts = Post.all
end
我
index.html.erb
视图
我也呈现这样,如果它不会有什么我想做能有人告诉我一个工作职位列表更好的方式来做到这一点?
# <some way for user to choose those defined scopes and render the new page would go here>
<% @posts.each do |post| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to post.title, post %>
<small> <%= post.body %> </small>
</h4>
</div>
</div>
<% end %>