2017-09-03 54 views
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 %> 

回答

0

基本上有两种方法可以做到这一点:

  1. 纯粹的前端,用js。如果您不想编写自己的数据表(例如DataTables),可以使用一些现有的带有排序功能的js插件。您可能需要创建一个表格,其中包含可用于此的不同属性。

  2. 后端,使用ajax。编写你自己的或使用像filterrific,它使用范围进行过滤和排序。