[英]How to use has_scope gem in a view in rails 4?
I'm trying to add a clickable link that will sort a page of links alphabetically using has_scope gem. 我正在尝试添加一个可点击的链接,该链接将使用has_scope gem按字母顺序对链接页面进行排序。 What exactly would i put in my view to make this work?
在我看来,我究竟想要做些什么呢?
Model Links.rb 型号Links.rb
scope :abc, -> { order("links.title ASC") }
Links_Controller.rb Links_Controller.rb
has_scope :abc
def index
@links = apply_scopes(Link).all
end
Index.html.erb Index.html.erb
<div id="links-wrapper">
<%= render partial: "shared/link", collection: @links %>
</div>
_link.html.erb _link.html.erb
<div class="link">
<a class="link-title" href="<%= link.url %>" target="_blank"><%= link.title %></a>
<div class="link-printed-url"><%= link.url %></div>
<p class="link-description"><%= link.description %></p>
<div class="link-tags">
<% link.tags.any? %>
<% link.tags.each do |tag| %>
<span class="label-tag">
<%= link_to tag_path(tag) do %>
#<%= tag.name %>
<% end %>
</span>
<% end %>
</div>
</div>
You need to pass a title param to the scope. 您需要将标题参数传递给范围。
Change the scope in the model to scope :abc, -> title { order("links.title ASC") }
将模型中的
scope :abc, -> title { order("links.title ASC") }
更改为scope :abc, -> title { order("links.title ASC") }
or 要么
scope :abc, -> title { order(title: :asc) }
You could do something like this in the partial 你可以在局部做这样的事情
<div class="link">
<a class="link-title" href="<%= link.url %>" target="_blank"><%= link.title %></a>
<div class="link-printed-url"><%= link.url %></div>
<p class="link-description"><%= link.description %></p>
</div>
<% end %>
<div class="link-tags">
<%= link_to 'Order ASC', tag_path(:abc => true) %>
</div>
由于has_scope使用url params,你需要在link_to上添加params
<%= link_to "title", links_path(abc: true) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.