单例的map.resource和命名约定
我对 ruby on Rails 比较陌生,所以这个问题可能很简单。 Rails 做了很多魔法,我不知道在哪里查找这些东西,因为我不知道应该归咎于框架的哪一部分。
我基本上做了 authlogic_example 并随后摆弄了代码。 我的routes.rb 看起来像这样
map.root :controller => "user_session", :action => "new" # optional, this just sets the root route
map.resources :users
map.resource :user_session
如您所见,我有一个名为user_session 的控制器。 user_session 具有三个操作新建、创建和销毁。我可以到达控制器,
localhost:3000/user_sessions/[new,destroy,create].
我也可以到达新操作以
localhost:3000/user_session/new
进行销毁或创建,我在这里收到路由错误。根据 文档 第一种情况应该是标准情况:“ map.resource 被赋予单数名称。默认控制器名称仍然取自复数名称。”
我现在的问题是 link_to 只采用控制器名称的单数,我只能到达 new,但不能销毁
<%= link_to "singular", :controller=>"user_session", :action=>"destroy" %>
#=> http://localhost:3000/user_session/destroy
<%= link_to "plural", :controller=>"user_sessions", :action=>"destroy" %>
#=> http://localhost:3000/user_session
这非常令人困惑,甚至不接近我的预期,但也会导致问题:我不能,
redirect_to :controller=>"user_sessions", :action=>"destroy"
因为我重定向到
http://localhost:3000/user_session
正如我已经提到的,我对 Rails 还很陌生,所以我可能还没有正确的思维方式。你能给我指出任何描述这种行为的东西吗?我该如何解决这个问题?
I am relatively new to ruby on rails, so this question might be easy. Rails does a lot of magic and I am not sure where to look up such things, because I don't know which part of the framework is to blame.
I basically did the authlogic_example and fiddled around with the code afterwards.
My routes.rb looks like this
map.root :controller => "user_session", :action => "new" # optional, this just sets the root route
map.resources :users
map.resource :user_session
As you can see, I have a controller called user_session. user_session has three actions new, create and destroy. I can reach the controllers at
localhost:3000/user_sessions/[new,destroy,create].
I can also reach the new action at
localhost:3000/user_session/new
for destroy or create I get a routing error here. According to the documentation the first case should be the standard one: "A singular name is given to map.resource. The default controller name is still taken from the plural name."
My problem now is that link_to only takes the singular of the controller name, where I can only reach new, but not destroy
<%= link_to "singular", :controller=>"user_session", :action=>"destroy" %>
#=> http://localhost:3000/user_session/destroy
<%= link_to "plural", :controller=>"user_sessions", :action=>"destroy" %>
#=> http://localhost:3000/user_session
This is pretty confusing and is not even close to what I expected, but also causes problems: I can't
redirect_to :controller=>"user_sessions", :action=>"destroy"
because I get redirected to
http://localhost:3000/user_session
As I already mentioned, I am pretty new to rails, so I might not have the right way of thinking yet. Can you point me to anything that describes this behaviour? How would I resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的行为是正确的。至少对于 RESTful 路由 而言,要采取的操作链接到请求类型。
http://localhost:3000/user_session 上的 POST 请求将创建一个会话。而同一 URI 上的 DELETE 请求将破坏会话。
如果您要映射资源,则应该使用方便的方法来抽象出大部分资源。
但是,map.resources 不提供销毁助手。因此,您要么必须做出一个或明确提及:method => :delete
我更喜欢命名路由版本,它位于 config/routes.rb
然后在我的视图中使用它:
The behaviour you describe is correct. At least for RESTful routing. Where the action to take is linked to the request type.
a POST request on http://localhost:3000/user_session will create a session. While a DELETE request on the same URI will destroy the session.
If you're mapping resources you should be using the convenience methods, to abstract most of that out.
However, map.resources doesn't provide a destroy helper. So you will either have to make one or explicitly mention :method => :delete
I prefer the named route version where this goes in config/routes.rb
Then just use this in my views: