Rails 控制器 - 获取索引操作以仅显示某些记录
默认情况下,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,当我们谈论过滤数据时,我更喜欢通过普通的旧 GET 变量(没有额外的路由定义)
url?key=val&key-val
来保持相同的索引操作和过滤参数。这有很多好处:
我不想创建额外的路由因为过滤器的复杂性很容易变得太高。如果过滤器参数很少并且您确定自己在做什么,您可以定义额外的好路由
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:
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.如果您只想显示一个品牌的汽车,那么最好的网址是:
/makes/1-Acura/cars
。因此,您只需在汽车控制器中获取该品牌的汽车即可。您是否有品牌表,或者只是汽车表中的一根绳子?我认为你应该拥有一个。
使用这些路线,您必须测试汽车控制器的索引操作中是否有 params[:make_id] ,如果是这种情况,您将得到这样的汽车:
设置您的路线
或者您可以像这样 这样,您就可以像这样设置控制器:
路径
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.
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:
Or you could set up your routes like that
This way, you can have your controllers setup like that:
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.