Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I set the default format, like JSON, for a customized Devise controller in Rails 3?
    primarykey
    data
    text
    <p>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.</p> <p>In my routes.rb file I have the following:</p> <pre><code>devise_for :users, :controllers =&gt; { :omniauth_callbacks =&gt; "users/omniauth_callbacks", :registrations =&gt; "users/registrations", :sessions =&gt; "users/sessions" } </code></pre> <p>That works as expected. My routes now show the custom controller routes like:</p> <pre><code>new_user_session GET /users/sign_in(.:format) { :action =&gt;"new", :controller =&gt;"users/sessions" } new_user_registration GET /users/sign_up(.:format) { :action=&gt;"new", :controller=&gt;"users/registrations" } </code></pre> <p>To set the default format for a resource I would do something like this:</p> <pre><code>resources :users, :defaults =&gt; { :format =&gt; 'json' } </code></pre> <p>So, I tried this:</p> <pre><code>namespace "users" do resources :registrations, :defaults =&gt; { :format =&gt; 'json' } resources :sessions, :defaults =&gt; { :format =&gt; 'json' } end </code></pre> <p>Which did not work as expected. I ended up with these routes:</p> <pre><code>new_users_registration GET /users/registrations/new(.:format) { :format=&gt;"json", :action=&gt;"new", :controller=&gt;"users/registrations" } new_users_session GET /users/sessions/new(.:format) { :format=&gt;"json", :action=&gt;"new", :controller=&gt;"users/sessions" } </code></pre> <p>In order for this to work with my custom overrides in Devise, I need to format 'new_user_registration' not 'new_users_registration'.</p> <p>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.</p> <p>Does anyone know of any routing magic that I can use to make this happen?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload