Rails 控制器 - 获取索引操作以仅显示某些记录

发布于 2024-12-21 01:08:07 字数 465 浏览 1 评论 0原文

默认情况下,rails 控制器将加载索引操作中的所有关联对象。我想做的是只显示某些对象。

例如,

我有一个名为 Car(id、make、model、year) 的模型。我想根据参数仅列出索引中的特定品牌。

有几种方法可以做到这一点,我只是不确定哪种最好。

我可以:

将参数传递给链接:

cars_path(make: 'Acura') 

并给我 /cars/?make=Acura

设置路线:(这似乎变得混乱)

match "cars/:make" => "cars#index", constraints: {make: /[A-z]{1,20}/}

或者我可以为此制定一个单独的控制器操作

关于什么是最“的建议Rails-y”的方式来做到这一点?回报率3.1

By default the rails controller will load all associated objects in the index action. What I would like to do is display only certain objects.

For example

I have a model called Car(id, make, model, year). I want list only particular makes in the index, depending on a parameter.

There are a few ways to do this, I'm just not sure which is best.

I could:

pass a parameter to the link:

cars_path(make: 'Acura') 

and would give me /cars/?make=Acura

set up routes: (this seems to get messy)

match "cars/:make" => "cars#index", constraints: {make: /[A-z]{1,20}/}

or I could make a separate controller action for this

Any suggestion about what is the most "rails-y" way to do this? RoR 3.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

忘年祭陌 2024-12-28 01:08:07

通常,当我们谈论过滤数据时,我更喜欢通过普通的旧 GET 变量(没有额外的路由定义)url?key=val&key-val 来保持相同的索引操作和过滤参数。

这有很多好处:

  • url 可以添加书签
  • 没有会话修补
  • 我可以重用过滤参数并将它们传递给分页链接,这样可以让过滤器跟随用户,而搜索是有序的

我不想创建额外的路由因为过滤器的复杂性很容易变得太高。如果过滤器参数很少并且您确定自己在做什么,您可以定义额外的好路由 url/param/param 但我发现这些情况很少甚至没有。

Usually, when we are talking of filtering data, I prefer to keep the same index action and filtering parameters via plain old GET vars (no extra route definitions) url?key=val&key-val.

This has a number of benefits among them:

  • url is bookmark-able
  • no session tinkering
  • I can reuse the filtering params and pass them to pagination links and such to have the filter follow the user while search is in order

I prefer not to make extra routes as the complexity of the filter can easily go too high. If the filter params are few and you are sure of what you are doing, you may define extra nice routes url/param/param but I find that those cases are few to none.

小矜持 2024-12-28 01:08:07

如果您只想显示一个品牌的汽车,那么最好的网址是:/makes/1-Acura/cars。因此,您只需在汽车控制器中获取该品牌的汽车即可。

您是否有品牌表,或者只是汽车表中的一根绳子?我认为你应该拥有一个。

resources :makes do
    resources :cars
end

使用这些路线,您必须测试汽车控制器的索引操作中是否有 params[:make_id] ,如果是这种情况,您将得到这样的汽车:

@cars = Make.find(params[:make_id]).cars

设置您的路线

resources :makes do
    scope :module => "make_scope" do
        resources :cars
    end
end

或者您可以像这样 这样,您就可以像这样设置控制器:

controllers
    - cars_controller.rb
    - make_scope (folder)
        - cars_controller.rb

路径 make_cars_path(@make) 将命中 make_scope/cars_controller 中的索引操作,因此您不必担心是否存在params[:make_id],您就会知道您正在使用某个品牌的汽车。

否则,获取参数就可以了。我认为定义一个新的路由来获取更漂亮的 URL 并不是坏事,具体取决于过滤器的复杂性。

If you just want to display the cars of one make, the best url imo would be: /makes/1-Acura/cars. So you would just get the cars of this make in the cars controller.

Do you have a table for makes or is it just a string in your car table? I think you should have one.

resources :makes do
    resources :cars
end

With these routes, you would have to test if there is a params[:make_id] in the index action of the cars controller, and if it's the case you would get the cars like that:

@cars = Make.find(params[:make_id]).cars

Or you could set up your routes like that

resources :makes do
    scope :module => "make_scope" do
        resources :cars
    end
end

This way, you can have your controllers setup like that:

controllers
    - cars_controller.rb
    - make_scope (folder)
        - cars_controller.rb

The path make_cars_path(@make) would hit the index action in the make_scope/cars_controller, so you would not have to worry about the presence of a params[:make_id], you would just know you're working with the cars of a make.

Otherwise, the get params are fine. I don't think it's bad to define a new route to get prettier urls though, depending on the complexity of your filters.

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