如何在Rails 3中为自定义Devise控制器设置默认格式(如JSON)? [英] How do I set the default format, like JSON, for a customized Devise controller in Rails 3?
问题描述
我的Rails 3应用程序为我的Devise + OmniAuth集成提供了三个自定义的控制器.我需要覆盖用于用户注册和会话的标准方法,例如"new".我特别需要控制器方法来处理与JSON格式兼容的重定向和响应.
My Rails 3 app has three customized controllers for my Devise + OmniAuth integration. I needed to override the standard methods, like 'new', for user registrations and sessions. I specifically needed the controller methods to handle redirects and responses that are compatible with JSON formatting.
在routes.rb文件中,我具有以下内容:
In my routes.rb file I have the following:
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks",
:registrations => "users/registrations",
:sessions => "users/sessions"
}
那可以按预期工作.我的路线现在显示自定义控制器路线,例如:
That works as expected. My routes now show the custom controller routes like:
new_user_session GET /users/sign_in(.:format) {
:action =>"new",
:controller =>"users/sessions"
}
new_user_registration GET /users/sign_up(.:format) {
:action=>"new",
:controller=>"users/registrations"
}
要为资源设置默认格式,我将执行以下操作:
To set the default format for a resource I would do something like this:
resources :users, :defaults => {
:format => 'json'
}
所以,我尝试了这个:
namespace "users" do
resources :registrations, :defaults => {
:format => 'json' }
resources :sessions, :defaults => {
:format => 'json' }
end
哪个没有按预期工作.我结束了这些路线:
Which did not work as expected. I ended up with these routes:
new_users_registration GET /users/registrations/new(.:format) {
:format=>"json",
:action=>"new",
:controller=>"users/registrations"
}
new_users_session GET /users/sessions/new(.:format) {
:format=>"json",
:action=>"new",
:controller=>"users/sessions"
}
为了使其能够与我在Devise中的自定义替代一起使用,我需要设置格式为"new_user_registration"而不是"new_users_registration".
In order for this to work with my custom overrides in Devise, I need to format 'new_user_registration' not 'new_users_registration'.
我检查了'devise_for'方法,它没有:defaults选项.我可以使用'devise_scope'方法来设置单独的路由,但这似乎远不如:defaults惯用语那么简单.
I checked the 'devise_for' method and it does not have a :defaults option. I can use the 'devise_scope' method to set the individual routes, but that seems far less concise that the :defaults idiom.
有人知道我可以用来实现这一目标的任何路由魔术吗?
Does anyone know of any routing magic that I can use to make this happen?
推荐答案
我找到了一个不一定令人满意的答案,但它可行.我在routes.rb中尝试过:
I found an answer that isn't necessarily satisfying, but it works. I tried this in routes.rb:
devise_scope :user do
get "sign_up", :to => "users/registrations#new",
:defaults => { :format => 'json' }
end
然后我在自定义控制器中尝试了此操作
And I tried this in my custom controllers:
redirect_to new_user_registration_url, :format => 'json'
都不起作用.我猜这两个在实现上都是不正确的.我终于在自定义控制器中使用了它:
Neither worked. I am guessing both of those were incorrect in implementation. I finally used this in my custom controllers:
redirect_to :controller => 'users/registrations',
:action => 'new',
:format => 'json'
这取代了我最初拥有的所有地方:
That replaced everywhere I originally had:
redirect_to new_user_registration_url
它比我喜欢的更冗长,并且不是很干,但是它可以工作.
It's more verbose than I like and not very DRY, but it works.
这篇关于如何在Rails 3中为自定义Devise控制器设置默认格式(如JSON)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!