在 Ruby on Rails 中通过 link_to 发送变量而不使用 url 查询
我试图通过 link_to 发送一个变量,而不使用 Ruby on Rails 中的 url 查询,从一个控制器的索引视图发送一个变量,用于另一个控制器的索引。
基本上,我需要将 Turma.id (学校班级 id)传递给 Elementos_turma(班级中的人员)控制器,这样我就可以使用它并过滤索引: ElementosTurma.find(:all, :conditions => { :turma_id => xxxxx } ),显示所选类别的人员。
有可能吗? 也许不使用会话变量? 也许将变量发送到第一个控制器上的方法,然后将其发送到另一个控制器? (如果是这样怎么办?不太明智……:))
I'm trying to send a variable through a link_to, without using url query in Ruby on Rails, from one controller's index view to be used on another controller's index.
Basically, I need to pass the Turma.id (school class id), to the Elementos_turma(peoples in the class) controller, so I can use it and filter the index: ElementosTurma.find(:all, :conditions => { :turma_id => xxxxx } ), to show the peoples of the selected class.
It it possible?
Maybe without using a session variable?
Maybe sending the variable to a method on the 1st controller, to send it the other controller? (if so how? not very RoR wise... :) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要特殊的方法来获取您需要的信息,路线的魔力就可以了。
在你的routes.db文件中,你应该能够定义这样的东西,
然后如果你运行'rake paths'你应该看到类似这样的路由
你可以在你的视图中调用该路径,就像这样
然后在你的控制器中你将有访问 params[:class_id]
路线的名称不是很漂亮,但这应该可以。
编辑--------------------------------------
根据您的评论,由于某种原因你不能使用map.resources...
这将在你的视图中产生相同的可用路线,在你的控制器中产生相同的参数。
话虽这么说,我不知道服务器错误如何禁止您使用 map.resources
No need for a special method to get the info you need, the magic of routes will do.
In your routes.db file, you should be able to define something like this,
Then if you run 'rake routes' you should see a routes similar to this
You can call that path in your view like so
Then in your controller you will have access to params[:class_id]
The name of the route isn't very pretty, but this should work.
EDIT--------------------------------------
According to your comment, you can't use map.resources for some reason or another...
That will produce the same route available in your view, same param in your controller.
That being said, I don't know how a server bug could prohibit you from using map.resources
如果不在链接中包含该数据,则无法通过链接传输数据。
然而,听起来您只需要使用嵌套资源。 (因为我会说英语,所以我不会处理另一种语言并做我想做的事。)如果您想保持 RESTful,那么您想要将人们发送到的 URL 可能应该看起来更像这样:
这就是“Rails way” 表明你想要让人们加入#1 类,Rails 提供了内置的路由方法来使这变得容易。请参阅 Rails 指南中的Rails 从外向内路由一文。
You can not transfer data through a link without including that data in the link.
However, it sounds like you just need to be using nested resources. (Since I speak English, I'm going to not tackle another language and do what comes to me.) The URLs you want to be sending people to probably should look more like this if you want to be RESTful:
That is the "Rails way" of indicating that you want to get people in class #1, and Rails offers built-in routing methods to make this easy. See the Rails Routing from the Outside In article in the Rails Guide.