Rails 中表的字母过滤器

发布于 2024-11-27 17:28:19 字数 1391 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2024-12-04 17:28:19

使用它来获取特定字母的结果列表:

visitors = Visitor.order("last_name ASC").where("last_name like '?'", letter)

然后在您看来:

<% @visitor_array.each do |visitor| %>

    <%= link_to visitor(visitor) %>

<% end %>

语法可能略有偏差,但基本思想是存在的。

我不完全确定你的最后一行是什么意思......

Use this to get the list of results for a specific letter:

visitors = Visitor.order("last_name ASC").where("last_name like '?'", letter)

Then on your view:

<% @visitor_array.each do |visitor| %>

    <%= link_to visitor(visitor) %>

<% end %>

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文