在 Ruby on Rails 中通过 link_to 发送变量而不使用 url 查询

发布于 2024-08-17 23:36:51 字数 331 浏览 4 评论 0原文

我试图通过 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 技术交流群。

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

发布评论

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

评论(2

久光 2024-08-24 23:36:51

不需要特殊的方法来获取您需要的信息,路线的魔力就可以了。

在你的routes.db文件中,你应该能够定义这样的东西,

map.resources :class, :has_many => :students

然后如果你运行'rake paths'你应该看到类似这样的路由

class_students GET /classes/:class_id/students(.:format)  {:controller=>"students", :action=>"index"}

你可以在你的视图中调用该路径,就像这样

class_students_path(class_id)

然后在你的控制器中你将有访问 params[:class_id]

路线的名称不是很漂亮,但这应该可以。

编辑--------------------------------------

根据您的评论,由于某种原因你不能使用map.resources...

 map.class_students '/:class_id/students', :controller => 'students', :action => 'index'

这将在你的视图中产生相同的可用路线,在你的控制器中产生相同的参数。

话虽这么说,我不知道服务器错误如何禁止您使用 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,

map.resources :class, :has_many => :students

Then if you run 'rake routes' you should see a routes similar to this

class_students GET /classes/:class_id/students(.:format)  {:controller=>"students", :action=>"index"}

You can call that path in your view like so

class_students_path(class_id)

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

 map.class_students '/:class_id/students', :controller => 'students', :action => 'index'

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

执手闯天涯 2024-08-24 23:36:51

如果不在链接中包含该数据,则无法通过链接传输数据。

然而,听起来您只需要使用嵌套资源。 (因为我会说英语,所以我不会处理另一种语言并做我想做的事。)如果您想保持 RESTful,那么您想要将人们发送到的 URL 可能应该看起来更像这样:

/classes/1/people/

这就是“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:

/classes/1/people/

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.

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