特定资源路由操作的别名(“/sign_in”而不是“/sessions/new”)
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于较新版本的 Rails,您不能再使用 match。而是使用 http 动词。
示例:
请注意,我使用了
get
而不是 match。在 Rails 4.0.0 上测试
For newer versions of Rails you can no longer use match. Instead use the http verb.
Example:
Note that i've used
get
instead of match.Tested on Rails 4.0.0
添加此行:
到您的
config/routes.rb
中。Add this line:
To your
config/routes.rb
.该
行创建一个名为“sign_in”的路由,您可以使用它来代替“new_session”。因此,在任何您链接到“new_session_path”的地方,请将其替换为“sign_in_path”。
这就足够了。不过,我最初希望做的事情会很好——让“new_session_path”链接到与“sign_in_path”相同的东西。能够指定不包含控制器的路径名。那么我根本不需要
match
行。The
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.“路径”属性怎么样?这应该做你想做的。
How about the "path" attribute? This should do what you want.