单例的map.resource和命名约定

发布于 2024-08-08 01:12:48 字数 1535 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(1

不…忘初心 2024-08-15 01:12:48

您所描述的行为是正确的。至少对于 RESTful 路由 而言,要采取的操作链接到请求类型。

http://localhost:3000/user_session 上的 POST 请求将创建一个会话。而同一 URI 上的 DELETE 请求将破坏会话。

如果您要映射资源,则应该使用方便的方法来抽象出大部分资源。

<%= link_to "Login", create_user_session_url %>

但是,map.resources 不提供销毁助手。因此,您要么必须做出一个或明确提及:method => :delete

<%= link_to "Logout", {:controller => "user_sessions", :action => :destroy}, :method => :destroy %>

我更喜欢命名路由版本,它位于 config/routes.rb

map.logout '/logout', :controller => "sessions" , :action => :destroy

然后在我的视图中使用它:

<%= link_to "Logout", logout_url %>

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.

<%= link_to "Login", create_user_session_url %>

However, map.resources doesn't provide a destroy helper. So you will either have to make one or explicitly mention :method => :delete

<%= link_to "Logout", {:controller => "user_sessions", :action => :destroy}, :method => :destroy %>

I prefer the named route version where this goes in config/routes.rb

map.logout '/logout', :controller => "sessions" , :action => :destroy

Then just use this in my views:

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