heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
24k stars 5.55k forks source link

Using other format for rendering #925

Closed ghost closed 13 years ago

ghost commented 13 years ago

Hello,

I tried somethings wich doesn't work and makes me crazy :-)

In the initializers/devise.rb, I added a new format for iPad (because I have a template especially for iPad) : config.navigationalformats = [:"/", "/_", :html, :ipad]

in my application_controller.rb, I detect if its an ipad and add the format: before_filter :adjust_format_for_ipad private def adjust_format_for_ipad
request.format = :ipad if ipad_user_agent? end

I created a view in devise/sessions/new.ipad.erb with the login form When I try to access to the url /my, it redirects me to /users/sign_in, the form is correctly rendered (good !) .

However after posting my credential, I get this message: Missing template devise/sessions/create with {:locale=>[:en, :en], :formats=>[:ipad], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder]} As I learned, it shouldn't have any template for the action 'create'. If I try to access to /my after the authentication, I get the right authenticated page.

Did I something wrong?

Thank you for your help

josevalim commented 13 years ago

Devise version and full backtrace please?

ghost commented 13 years ago

Version : 1.1.8

ActionView::MissingTemplate (Missing template devise/sessions/create with {:locale=>[:en, :en], :formats=>[:ipad], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder]} in view paths "/app/views", "/vendor/plugins/plugins/app/views", "/vendor/plugins/acts_as_tree/app/views", "/opt/local/lib/ruby/gems/1.8/gems/client_side_validations-3.0.0.alpha.9/app/views", ".bundler/ruby/1.8/devise-74166e224b4d/app/views"):

Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.8ms)

josevalim commented 13 years ago

Hrm, I have no idea what is happening yet. Another question: this error when your credentials are valid and invalid? Or just one of them? Can you sign in normally outside the ipad format?

ghost commented 13 years ago

Only when credentials are valid, when it's invalid it goes back to the form. I works normally in ':html' mode

josevalim commented 13 years ago

Weird issue to happen on 1.1.8. Although understandable if it was happening on master. Look at v1.1.8 code:

https://github.com/plataformatec/devise/blob/v1.1.8/app/controllers/devise/sessions_controller.rb#L15

However, master calls respond_with, which will trigger your responder. Try switching to Devise master and doing the following in an initializer:

ActionController::Responder.class_eval do
  alias :to_html :to_ipad
end
ghost commented 13 years ago

I'm not sure if I'm doing this right: I did a gem update devise (initially installed through the gemfile, using the master url) I created an initializer in the folder initializers called action_controller.rb and pasted your code.

I got this message when I 'rails s'

/config/initializers/action_controller.rb:2: undefined method ipad' for classActionController::Responder' (NameError) from /config/initializers/action_controller.rb:1:in `class_eval' from /config/initializers/action_controller.rb:1

josevalim commented 13 years ago

Sorry, my code was wrong. I never get the alias order right. :S Try this:

ActionController::Responder.class_eval do
  alias :to_ipad :to_html
end
ghost commented 13 years ago

huuu ! Thank you very much ! it works!

josevalim commented 13 years ago

Glad to help! Could you please add a new section to the Wiki with a title like: "How To: Make Devise work with other formats like mobile, iphone and ipad" and describe the steps you did? Basically what you described at the beginning of this issue and the hack I gave you.

Then other developers in the future will have a guideline to follow! Thanks a lot! :D

ghost commented 13 years ago

I tried my best :-), I'm not good a writing doc :-)

swrobel commented 13 years ago

I'm really confused about why this is all necessary considering that there aren't html versions of the views for create. If :mobile is included in the navigational formats, why do we need to include this code?

ActionController::Responder.class_eval do
  alias :to_mobile :to_html
end
ghost commented 13 years ago

In my case, the iPad doesn't have the same content as the iPhone. This is why I had to separate

paulnicholson commented 13 years ago

any word on why you need the alias after adding to the navigational formats?

josevalim commented 13 years ago

It is a rails requirement. If you don't understand how responders work, try reading more about it. Like docs and so on.

paulnicholson commented 13 years ago

I understand responders. I asking why devise doesn't set that up for you using the navigational formats array.

josevalim commented 13 years ago

I wouldn't assume it is Devise's responsibility to setup your application responder.

swrobel commented 13 years ago

Perhaps I'm a little uninformed, but I have my :mobile format set up and my mobile views all work fine. The question for me is why Devise won't respond to :mobile format for actions that simply redirect without adding the following to an initializer:

ActionController::Responder.class_eval do
  alias :to_mobile :to_html
end
josevalim commented 13 years ago

Because Devise uses respond_with internally and, if you don't have an specific format registered in the responder (and in respond_with), that format can't be called in the first place.

ketanjm commented 12 years ago

HI, im using rails3.1

gem 'mobylette' gem 'jquery_mobile_rails'

and added these configuration in initializers

in new .rb file in initializers

ActionController::Responder.class_eval do alias :to_mobile :to_html end

And

config.navigational_formats = ["*/*", :html, :mobile]

but not getting mobile views still error

ActionView::MissingTemplate (Missing template users/sessions/new, devise/sessions/new, devise/new, application/new with {:handlers=>[:erb, :builder, :coffee], :formats=>[:mobile], :locale=>[:en, :en]}. Searched in:

and other templates for which i created .mobile.erb working fine

what should i check

ketanjm commented 12 years ago

Done!!!, Actually I didn't created mobile views for devise templates !!!:)

parkr commented 12 years ago

So @josevalim, you seem to have been a helpful source here thus far.

I'm in a similar boat, but when I aliased :to_mobile to :to_html, I got the incorrect views (the *.html.erb ones, to be exact. I want the *.mobile.erb ERBs). I've done everything else suggested, but am stuck. I get the error of a missing template for devise/sessions/create, which I definitely shouldn't get, ever.

ketanjm commented 12 years ago

try this gem 'mobylette'

ketanjm commented 12 years ago

try this gem 'mobylette'

ketanjm commented 12 years ago

try this gem 'mobylette'

ketanjm commented 12 years ago

try this gem 'mobylette' and set temp at config/initializers/devise.rb

config.navigationalformats = ["/_", :html, :mobile]

ketanjm commented 12 years ago

try this gem 'mobylette' and set at config/initializers/devise.rb

config.navigationalformats = ["/_", :html, :mobile]

parkr commented 12 years ago

That doesn't seem to work. Here's my devise.rb file.

ketanjm commented 12 years ago

its may be the version problem , check the latest versions and follow correct instruction given on README's its working perfectly for my application .

dankozlowski commented 12 years ago

@parkr I'm getting the same issue. Were you able to resolve?

parkr commented 12 years ago

@dankozlowski I added the following to the bottom of my config/initializers/devise.rb file:

ActionController::Responder.class_eval do
  alias :to_mobile :to_html
end