特定资源路由操作的别名(“/sign_in”而不是“/sessions/new”)

发布于 2024-09-15 02:03:55 字数 737 浏览 0 评论 0原文

我在 Rails 3 中设置了安静的身份验证和用户管理,没有任何 gem。

但是,我认为需要转到“/sessions/new”而不是“/sign_in”是愚蠢的。

我知道您可以为整个资源添加别名,这样我的用户就可以使用“/squirrels”-and-friends 来代替“/sessions”-and-friends,但这不是我想要的在这里完成。我想为一项特定操作指定别名。

我知道这可以有点来完成

resources :sessions, :path_names => { :new => "sign_in" }

,但是路线最终会变成“/sessions/sign_in”——而且我根本不希望控制器名称出现在这个操作中。我希望我可以用

resources :sessions, :path_names => { :new => "/sign_in" }

“/”来指定它,告诉rails它是一个完整的路径名。但这与第一个代码片段具有相同的效果。

我的最后一次尝试只是使用表面的方式

match "sign_in" => "sessions#new"

,允许某人在 URL 栏中手动输入“/sign_in”,但是使用 new_session_(path|url) 生成的链接仍然会让用户处于更尴尬的“/会话/登录”。

I set up restful authentication and user management without any gems in Rails 3.

However, I think it's silly to need to go to "/sessions/new" instead of "/sign_in".

I know you can alias an entire resource, so that instead of "/sessions"-and-friends my users could use "/squirrels"-and-friends, but that's not what I'm trying to accomplish here. I want to alias one specific action.

I know this can kind of be accomplished with

resources :sessions, :path_names => { :new => "sign_in" }

but then the route ends up as "/sessions/sign_in" — and I don't want the controller name in there at all for this action. I wish I could specify this with

resources :sessions, :path_names => { :new => "/sign_in" }

where the "/" tells rails that it is a complete path name. But that has the same effect as the first code fragment.

My last attempt was just to use the superficial

match "sign_in" => "sessions#new"

which allows someone to manually enter "/sign_in" in their URL bar, but links made with new_session_(path|url) still land users at the more awkward "/sessions/sign_in".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

南烟 2024-09-22 02:03:55
match "sign_in" => "sessions#new", :as => :new_session
match "sign_in" => "sessions#new", :as => :new_session
明明#如月 2024-09-22 02:03:55

对于较新版本的 Rails,您不能再使用 match。而是使用 http 动词。

示例:

get "sign_in" => "sessions#new", :as => :new_session

请注意,我使用了 get 而不是 match。

在 Rails 4.0.0 上测试

For newer versions of Rails you can no longer use match. Instead use the http verb.

Example:

get "sign_in" => "sessions#new", :as => :new_session

Note that i've used get instead of match.

Tested on Rails 4.0.0

忆梦 2024-09-22 02:03:55

添加此行:

map.sign_in '/sign_in', :controller => 'session', :action => 'new'

到您的 config/routes.rb 中。

Add this line:

map.sign_in '/sign_in', :controller => 'session', :action => 'new'

To your config/routes.rb.

月亮坠入山谷 2024-09-22 02:03:55

match "sign_in" => "sessions#new"

行创建一个名为“sign_in”的路由,您可以使用它来代替“new_session”。因此,在任何您链接到“new_session_path”的地方,请将其替换为“sign_in_path”。

这就足够了。不过,我最初希望做的事情会很好——让“new_session_path”链接到与“sign_in_path”相同的东西。能够指定不包含控制器的路径名。那么我根本不需要 match 行。

The

match "sign_in" => "sessions#new"

line creates a route called "sign_in" which you can use in place of "new_session". So anywhere you had been linking to things with "new_session_path", replace it with "sign_in_path".

That's good enough. What I was originally hoping to do would be nice, though--to have "new_session_path" link to the same thing as "sign_in_path". To be able to specify a path_name that doesn't include the controller in it. Then I wouldn't need the match line at all.

や莫失莫忘 2024-09-22 02:03:55

“路径”属性怎么样?这应该做你想做的。

devise_for :users,
path: '',
controllers: {
  sessions: "user/sessions",
  passwords: "user/passwords",
  confirmations: "user/confirmations",
  registrations: "user/registrations"
},
path_names: {
  sign_in: "login",
  sign_out: "logout",
}

How about the "path" attribute? This should do what you want.

devise_for :users,
path: '',
controllers: {
  sessions: "user/sessions",
  passwords: "user/passwords",
  confirmations: "user/confirmations",
  registrations: "user/registrations"
},
path_names: {
  sign_in: "login",
  sign_out: "logout",
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文