comfy / comfortable-mexican-sofa

ComfortableMexicanSofa is a powerful Ruby on Rails 5.2+ CMS (Content Management System) Engine
MIT License
2.72k stars 638 forks source link

NoMethodError in CmsContentController#render_html #75

Closed lgs closed 13 years ago

lgs commented 13 years ago

I've a Rails (3.0.9) app with omniauth (0.2.6) in place, after integrating cms I get in my browser :

NoMethodError in CmsContentController#render_html
undefined method `gsub!' for nil:NilClass

while in rails log seems to mismatch the authentication routing :

Started GET "/auth/github" for 127.0.0.1 at 2011-07-25 21:04:46 +0200
  Processing by CmsContentController#render_html as HTML
  Parameters: {"cms_path"=>"auth/github"}
  SQL (0.3ms)  SELECT COUNT(*) FROM "cms_sites"
  Cms::Site Load (0.3ms)  SELECT "cms_sites".* FROM "cms_sites" LIMIT 1
Completed 500 Internal Server Error in 156ms

NoMethodError (undefined method `gsub!' for nil:NilClass):

... at first time integration, seems it was fine, but after loggin out/in with omniauth application ( github authenticated ) this error appeared.

The cms-admin authentication works fine instead.
My Gemfile.lock is here: http://pastie.org/2270005

Any help will be greatly appreciated. Luca G. Soave

GBH commented 13 years ago

Do you mind posting rake routes output and the full backtrace?

lgs commented 13 years ago

... sure, the routes are here :

lsoave@ubuntu:~/rails/github/gitwatcher$ rake routes
MongoDB logging. Please note that logging negatively impacts performance and should be disabled for high-performance production apps.
MONGODB admin['$cmd'].find({:ismaster=>1})
MONGODB admin['$cmd'].find({:ismaster=>1})
                                  root          /(.:format)                                                                   {:controller=>"home", :action=>"index"}
                              watchers          /watchers(.:format)                                                           {:controller=>"home", :action=>"index"}
            by_n_forks_and_last_pushed          /by_n_forks_and_last_pushed(.:format)                                         {:controller=>"home", :action=>"by_n_forks_and_last_pushed"}
         by_new_created_and_n_watchers          /by_new_created_and_n_watchers(.:format)                                      {:controller=>"home", :action=>"by_new_created_and_n_watchers"}
         by_last_pushed_and_n_watchers          /by_last_pushed_and_n_watchers(.:format)                                      {:controller=>"home", :action=>"by_last_pushed_and_n_watchers"}
                       superfeedr_pshb GET|POST /superfeedr/pshb(.:format)                                                    {:controller=>"superfeedr", :action=>"pshb"}
                               signout          /signout(.:format)                                                            {:controller=>"sessions", :action=>"destroy"}
                                                /auth/:provider/callback(.:format)                                            {:controller=>"sessions", :action=>"create"}
                             cms_admin GET      /cms-admin(.:format)                                                          {:controller=>"cms_admin/base", :action=>"jump"}
       form_blocks_cms_admin_site_page          /cms-admin/sites/:site_id/pages/:id/form_blocks(.:format)                     {:action=>"form_blocks", :controller=>"cms_admin/pages"}
     toggle_branch_cms_admin_site_page          /cms-admin/sites/:site_id/pages/:id/toggle_branch(.:format)                   {:action=>"toggle_branch", :controller=>"cms_admin/pages"}
          reorder_cms_admin_site_pages          /cms-admin/sites/:site_id/pages/reorder(.:format)                             {:action=>"reorder", :controller=>"cms_admin/pages"}
   revert_cms_admin_site_page_revision PUT      /cms-admin/sites/:site_id/pages/:page_id/revisions/:id/revert(.:format)       {:action=>"revert", :controller=>"cms_admin/revisions"}
         cms_admin_site_page_revisions GET      /cms-admin/sites/:site_id/pages/:page_id/revisions(.:format)                  {:action=>"index", :controller=>"cms_admin/revisions"}
          cms_admin_site_page_revision GET      /cms-admin/sites/:site_id/pages/:page_id/revisions/:id(.:format)              {:action=>"show", :controller=>"cms_admin/revisions"}
                  cms_admin_site_pages GET      /cms-admin/sites/:site_id/pages(.:format)                                     {:action=>"index", :controller=>"cms_admin/pages"}
                                       POST     /cms-admin/sites/:site_id/pages(.:format)                                     {:action=>"create", :controller=>"cms_admin/pages"}
               new_cms_admin_site_page GET      /cms-admin/sites/:site_id/pages/new(.:format)                                 {:action=>"new", :controller=>"cms_admin/pages"}
              edit_cms_admin_site_page GET      /cms-admin/sites/:site_id/pages/:id/edit(.:format)                            {:action=>"edit", :controller=>"cms_admin/pages"}
                   cms_admin_site_page PUT      /cms-admin/sites/:site_id/pages/:id(.:format)                                 {:action=>"update", :controller=>"cms_admin/pages"}
                                       DELETE   /cms-admin/sites/:site_id/pages/:id(.:format)                                 {:action=>"destroy", :controller=>"cms_admin/pages"}
                cms_admin_site_uploads POST     /cms-admin/sites/:site_id/uploads(.:format)                                   {:action=>"create", :controller=>"cms_admin/uploads"}
                 cms_admin_site_upload DELETE   /cms-admin/sites/:site_id/uploads/:id(.:format)                               {:action=>"destroy", :controller=>"cms_admin/uploads"}
 revert_cms_admin_site_layout_revision PUT      /cms-admin/sites/:site_id/layouts/:layout_id/revisions/:id/revert(.:format)   {:action=>"revert", :controller=>"cms_admin/revisions"}
       cms_admin_site_layout_revisions GET      /cms-admin/sites/:site_id/layouts/:layout_id/revisions(.:format)              {:action=>"index", :controller=>"cms_admin/revisions"}
        cms_admin_site_layout_revision GET      /cms-admin/sites/:site_id/layouts/:layout_id/revisions/:id(.:format)          {:action=>"show", :controller=>"cms_admin/revisions"}
                cms_admin_site_layouts GET      /cms-admin/sites/:site_id/layouts(.:format)                                   {:action=>"index", :controller=>"cms_admin/layouts"}
                                       POST     /cms-admin/sites/:site_id/layouts(.:format)                                   {:action=>"create", :controller=>"cms_admin/layouts"}
             new_cms_admin_site_layout GET      /cms-admin/sites/:site_id/layouts/new(.:format)                               {:action=>"new", :controller=>"cms_admin/layouts"}
            edit_cms_admin_site_layout GET      /cms-admin/sites/:site_id/layouts/:id/edit(.:format)                          {:action=>"edit", :controller=>"cms_admin/layouts"}
                 cms_admin_site_layout PUT      /cms-admin/sites/:site_id/layouts/:id(.:format)                               {:action=>"update", :controller=>"cms_admin/layouts"}
                                       DELETE   /cms-admin/sites/:site_id/layouts/:id(.:format)                               {:action=>"destroy", :controller=>"cms_admin/layouts"}
revert_cms_admin_site_snippet_revision PUT      /cms-admin/sites/:site_id/snippets/:snippet_id/revisions/:id/revert(.:format) {:action=>"revert", :controller=>"cms_admin/revisions"}
      cms_admin_site_snippet_revisions GET      /cms-admin/sites/:site_id/snippets/:snippet_id/revisions(.:format)            {:action=>"index", :controller=>"cms_admin/revisions"}
       cms_admin_site_snippet_revision GET      /cms-admin/sites/:site_id/snippets/:snippet_id/revisions/:id(.:format)        {:action=>"show", :controller=>"cms_admin/revisions"}
               cms_admin_site_snippets GET      /cms-admin/sites/:site_id/snippets(.:format)                                  {:action=>"index", :controller=>"cms_admin/snippets"}
                                       POST     /cms-admin/sites/:site_id/snippets(.:format)                                  {:action=>"create", :controller=>"cms_admin/snippets"}
            new_cms_admin_site_snippet GET      /cms-admin/sites/:site_id/snippets/new(.:format)                              {:action=>"new", :controller=>"cms_admin/snippets"}
           edit_cms_admin_site_snippet GET      /cms-admin/sites/:site_id/snippets/:id/edit(.:format)                         {:action=>"edit", :controller=>"cms_admin/snippets"}
                cms_admin_site_snippet PUT      /cms-admin/sites/:site_id/snippets/:id(.:format)                              {:action=>"update", :controller=>"cms_admin/snippets"}
                                       DELETE   /cms-admin/sites/:site_id/snippets/:id(.:format)                              {:action=>"destroy", :controller=>"cms_admin/snippets"}
                       cms_admin_sites GET      /cms-admin/sites(.:format)                                                    {:action=>"index", :controller=>"cms_admin/sites"}
                                       POST     /cms-admin/sites(.:format)                                                    {:action=>"create", :controller=>"cms_admin/sites"}
                    new_cms_admin_site GET      /cms-admin/sites/new(.:format)                                                {:action=>"new", :controller=>"cms_admin/sites"}
                   edit_cms_admin_site GET      /cms-admin/sites/:id/edit(.:format)                                           {:action=>"edit", :controller=>"cms_admin/sites"}
                        cms_admin_site PUT      /cms-admin/sites/:id(.:format)                                                {:action=>"update", :controller=>"cms_admin/sites"}
                                       DELETE   /cms-admin/sites/:id(.:format)                                                {:action=>"destroy", :controller=>"cms_admin/sites"}
                               cms_css GET      /cms-css/:site_id/:layout_slug(.:format)                                      {:controller=>"cms_content", :action=>"render_css"}
                                cms_js GET      /cms-js/:site_id/:layout_slug(.:format)                                       {:controller=>"cms_content", :action=>"render_js"}
                              cms_html GET      /(*cms_path)                                                                  {:controller=>"cms_content", :action=>"render_html"}
lsoave@ubuntu:~/rails/github/gitwatcher$ 

... the last one

cms_html GET      /(*cms_path)
seems to get in conflict with omniauth gem (I think) ...

but the backtrace ( the only broken part is my old "github via omniauth" authentication ) is almost there :

Started GET "/" for 127.0.0.1 at 2011-07-25 21:04:44 +0200
  Processing by HomeController#index as HTML
Rendered home/index.html.haml within layouts/application (30.5ms)
Completed 200 OK in 47ms (Views: 46.0ms | ActiveRecord: 0.0ms)

Started GET "/auth/github" for 127.0.0.1 at 2011-07-25 21:04:46 +0200
  Processing by CmsContentController#render_html as HTML
  Parameters: {"cms_path"=>"auth/github"}
  SQL (0.3ms)  SELECT COUNT(*) FROM "cms_sites"
  Cms::Site Load (0.3ms)  SELECT "cms_sites".* FROM "cms_sites" LIMIT 1
Completed 500 Internal Server Error in 156ms

NoMethodError (undefined method `gsub!' for nil:NilClass):

All CMS functions are actually working fine, I can login cms-admin, manage the admin tool, logoff, see my testing post fine ... the only broken thing is omniauth login.

lgs commented 13 years ago

... I found something similar :

https://github.com/twg/comfortable-mexican-sofa/pull/14
but my "sofa" should be already "pached" ...

lsoave@ubuntu:~/rails/github/gitwatcher$ grep sofa Gemfile.lock
    comfortable_mexican_sofa (1.3.5)
  comfortable_mexican_sofa
lsoave@ubuntu:~/rails/github/gitwatcher$
GBH commented 13 years ago

I used omniauth with Sofa a while ago, didn't see issues like these. Looking at the routes it kinda makes sense that /auth/github hits cms pages. There's only /auth/github/callback registered. So it hits the catch-all route...

Try removing comfortable_mexican_sofa gem and simply put in the catch-all route like match '*' => 'controller#action' and see if omniauth still freaks out.

Not sure about the gsub! error. I need to see the line that triggers it.

lgs commented 13 years ago

removing

omfortable_mexican_sofa gem
and
config/initializers/comfortable_mexican_sofa.rb
everything works fine again.

Follow the same "click flow" as before:

lsoave@ubuntu:~/rails/github/gitwatcher$ rails s -p3001
=> Booting Mongrel
=> Rails 3.0.9 application starting in development on http://0.0.0.0:3001
=> Call with -d to detach
=> Ctrl-C to shutdown server
MongoDB logging. Please note that logging negatively impacts performance and should be disabled for high-performance production apps.
MONGODB admin['$cmd'].find({:ismaster=>1})
MONGODB admin['$cmd'].find({:ismaster=>1})

Started GET "/" for 127.0.0.1 at 2011-07-25 22:27:26 +0200
  Processing by HomeController#index as HTML
Rendered home/index.html.haml within layouts/application (29.5ms)
Completed 200 OK in 44ms (Views: 42.8ms | ActiveRecord: 0.0ms)

Started GET "/auth/github" for 127.0.0.1 at 2011-07-25 22:27:35 +0200
MONGODB gitwatch_dev['users'].find({:provider=>"github", :uid=>1573})

Started GET "/auth/github/callback?code=47e4babf56e5fec19ddd" for 127.0.0.1 at 2011-07-25 22:27:36 +0200
  Processing by SessionsController#create as HTML
  Parameters: {"code"=>"47e4babf56e5fec19ddd", "provider"=>"github"}
Redirected to http://localhost:3001/
Completed 302 Found in 255ms
MONGODB gitwatch_dev['users'].find({:_id=>BSON::ObjectId('4e23114b1d41c80f180005b2')})

Started GET "/" for 127.0.0.1 at 2011-07-25 22:27:39 +0200
  Processing by HomeController#index as HTML
Rendered home/index.html.haml within layouts/application (415.6ms)
Completed 200 OK in 890ms (Views: 428.6ms | ActiveRecord: 0.0ms)

as you can see I can authenticate with github via omniauth again ... because

Started GET "/auth/github" 
doesn't go through
{"cms_path"=>"auth/github"}
routing anymore ..

GBH commented 13 years ago

Right, but now put in the catch-all route match '*' => 'my_controller#my_action'and try again. Sofa's route hijacks /auth/github, I'm pretty sure it will be the same for the regular catch-all route.

GBH commented 13 years ago

I think I know what the problem is. Omniauth catches 404 responses and then does whatever it does. So catch-all routes effectively kill that.... Although Sofa should respond with 404 instead of blowing up with gsub! error. Seems that RefineryCMS has the same problem: http://groups.google.com/group/refinery-cms/browse_thread/thread/1fd21eb30d3bd9e0

Comment somebody left there:

What I did was create the following route:

match '/auth/:provider' => 'errors#error'

Then on the error action in the errors_controller I have:

render :file => "#{Rails.root}/public/404.html", :status => 404, :layout =>
false

Which is going to trigger a 404 error for omniauth to catch. 

Hope this helps

lgs commented 13 years ago

... about the first try you told me, the "catch-all route", I had this config/routes.rb :

Rails3MongoidOmniauthSimple::Application.routes.draw do

  root :to => "home#index"

  match "/watchers" => "home#index"
  match "/by_n_forks_and_last_pushed" => "home#by_n_forks_and_last_pushed"
  match "/by_new_created_and_n_watchers" => "home#by_new_created_and_n_watchers"
  match "/by_last_pushed_and_n_watchers" => "home#by_last_pushed_and_n_watchers"

  #match "/superfeedr/callback" => "superfeedr#callback"

  match "/superfeedr/pshb" => "superfeedr#pshb", :via => [:get, :post]

  match "/signout" => "sessions#destroy", :as => :signout
  match "/auth/:provider/callback" => "sessions#create"
end

... did you mean putting

match '*' => 'sessions#create'
on top of the file ?

GBH commented 13 years ago

No, bottom. Seems that omniauth wants the route to fall through so it triggers a 404. Catch-all route will catch everything. http://guides.rubyonrails.org/routing.html#route-globbing

lgs commented 13 years ago

I didn't make extensive route testing but, ... until now seems it works.

I applied Federico Gonzalez as you previously suggested.

I was also able to deploy a working version on heroku ( http://gitwatcher.com/blog ) ... it's like a charm !

Thank you for your previous support and << My compliments for the project ! >>

It seems very usable and useful. bye Luca G. Soave

GBH commented 13 years ago

Cool, glad it's working for you. I'll check why sofa's 404 response doesn't do the same thing.