Rails 中表的字母过滤器
我是 Rails 新手,所以如果有一个简单的答案,请原谅我。我正在尝试为 Rails 中的表格实现“字母索引”。我希望能够单击一个字母并刷新表 - 仅包含 last_name
列的第一个字母与单击的字母匹配的数据。
我的第一个想法是从数据库生成一个字母数组
@visitor_array = []
@v = Visitor.all
@v.each do |visitor|
@visitor_array << visitor.last_name[0,1]
end
@visitor_array.sort!
@visitor_array.uniq!
此代码给了我一个数组,其中每个第一个字母都有一个。我用它作为我的“字母表”。在我看来,我有以下代码
<% @visitor_array.each do |visitor| %>
<%= link_to visitor, alphasort(visitor) %>
<% end %>
这里的想法是在单击该字母时运行辅助函数。但这就是我被困住的地方。
更新:
好的,感谢下面的评论,我能够弄清楚了。如果其他人想知道,这就是它是如何完成的。
控制器
# Create array consisting of the first letter of every visitor's last name
@visitor_array = []
@v = Visitor.all
@v.each do |visitor|
@visitor_array << visitor.last_name[0,1]
end
#Sort array alphabetically in ASC order
@visitor_array.sort!
#Remove duplicate elements
@visitor_array.uniq!
if (params[:letter] != nil)
@visitors = Visitor.order("last_name ASC").where("last_name like ?", params[:letter] +"%" )
else
@visitors = Visitor.order("last_name ASC")
end
查看
<% @visitor_array.each do |letter| %>
<%= link_to letter, :controller => "visitors" , :letter => letter %>
<% end %>
I'm new to rails, so forgive me if there is an easy answer. I'm trying to implement an "Alphabet Index" for a table in rails. I want to be able to click on a letter and have the table refresh - containing only the data on which the first letter of the last_name
column matches the letter clicked.
My first thought was to generate an array of letter from the database
@visitor_array = []
@v = Visitor.all
@v.each do |visitor|
@visitor_array << visitor.last_name[0,1]
end
@visitor_array.sort!
@visitor_array.uniq!
This code gives me an array which has one of each of the first letters. I used this as my "alphabet". On my view I have the following code
<% @visitor_array.each do |visitor| %>
<%= link_to visitor, alphasort(visitor) %>
<% end %>
The thought here is to run a helper function when the letter is clicked. But here is where I'm stuck.
UPDATE:
Okay, thanks to the comments below, I was able to figure it out. In case anyone else was wondering, this is how it was accomplished.
CONTROLLER
# Create array consisting of the first letter of every visitor's last name
@visitor_array = []
@v = Visitor.all
@v.each do |visitor|
@visitor_array << visitor.last_name[0,1]
end
#Sort array alphabetically in ASC order
@visitor_array.sort!
#Remove duplicate elements
@visitor_array.uniq!
if (params[:letter] != nil)
@visitors = Visitor.order("last_name ASC").where("last_name like ?", params[:letter] +"%" )
else
@visitors = Visitor.order("last_name ASC")
end
VIEW
<% @visitor_array.each do |letter| %>
<%= link_to letter, :controller => "visitors" , :letter => letter %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用它来获取特定字母的结果列表:
然后在您看来:
语法可能略有偏差,但基本思想是存在的。
我不完全确定你的最后一行是什么意思......
Use this to get the list of results for a specific letter:
Then on your view:
The syntax might be slightly off but the fundamental idea is there.
I'm not entirely sure what you mean by the last line though...