Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refinery cms integrate into existing app, devise routes has problem #2406

Closed
raymondos opened this issue Sep 14, 2013 · 20 comments
Closed

refinery cms integrate into existing app, devise routes has problem #2406

raymondos opened this issue Sep 14, 2013 · 20 comments

Comments

@raymondos
Copy link

Hi, refinerycms integration into an app which has contains a devise as user authentication system is done, but I encounter a problem.

I mount the refinery engine in my routes.rb: mount Refinery::Core::Engine, :at => '/blogs'

My application has header with new_user_session_path and new_user_registration_path or destroy_user_session_path for all the pages, also for the refinery pages. I got an undefined local variable or method "new_user_session_path" for #<#<Class:0x007f9a686c8388>:0x007f9a686d29f0> error when open refinery pageshttp://localhost:3000/blogs.

After find some discussion in google groups, I follow the suggestion add main_app prefix in devise path, it works! I can open the refinery homepage. but a new issue comes out when I open http://localhost:3000/blogs/refinery:

NoMethodError in Refinery::Admin::DashboardController#index

undefined method `signup_path' for #<ActionDispatch::Routing::RoutesProxy:0x007f9a6864b928>

I have no idea how to handle this, any suggestion will be appreciated!

PS: I had followed the instruction in with-an-existing-rails-31-devise-app and added refinery_patch .rb restrict_refinery_to_refinery_users.rb into lib/refinery and then loaded it in application.rb. it says "it will just consist in telling refinery to use your devise helpers" but not working!

@parndt
Copy link
Member

parndt commented Sep 15, 2013

Did you add a route to signup_path? Refinery won’t be providing anything like that because refinerycms-authentication won’t have been added.

@raymondos
Copy link
Author

Nope.Should I have to add refinery-authentication module or just routing signup_path to my devise routes?
Btw, I still do not understand why devise route helper not working in my case?

@parndt
Copy link
Member

parndt commented Sep 15, 2013

Don’t add refinerycms-authentication, just route to your signup_path

@raymondos
Copy link
Author

Hi @parndt , could u pls give me some example about how to route refinery engine's signup_path to main_app.new_user_registration_path ? I am not familiar with that and try some ways but all failed.

@raymondos
Copy link
Author

I try the below in routes.rb

Refinery::Core::Engine.routes.prepend do
  match '/signup' => '#{main_app}/devise/registrations#new'
end

the signup_path works but new problem occur:

Routing Error

uninitialized constant Refinery::Controller
Try running rake routes for more information on available routes.

I have no idea where the problem is

@parndt
Copy link
Member

parndt commented Sep 18, 2013

Can you use a redirect route instead? Does anything already work?

@raymondos
Copy link
Author

I mount it on /blogs and I can see the refinery home page in localhost:300/blogs , but when I open the dashboard page localhost:3000/blogs/refinery refinery dashboard redirect to signup page, the problem occurs. My login account is title: "Superuser" already.
I guess maybe two probles in my case:

  1. refinery engine can not get user login session in main_app so that it asks me to signup.
  2. match '/signup' => '#{main_app}/devise/registrations#new' has problems, refinery engine does not know which controller to use.

@parndt
Copy link
Member

parndt commented Sep 18, 2013

Do you have a full stack trace from this error:

NoMethodError in Refinery::Admin::DashboardController#index

undefined method `signup_path' for #<ActionDispatch::Routing::RoutesProxy:0x007f9a6864b928>

Including the parts you think may not be relevant. :)

@raymondos
Copy link
Author

There is the full trace info :)

NoMethodError in Refinery::Admin::DashboardController#index

undefined method `signup_path' for #<ActionDispatch::Routing::RoutesProxy:0x007fd2169608f8>
Rails.root: /Users/admin/Work/Projects/Rails/myapprails

Application Trace | Framework Trace | Full Trace
actionpack (3.2.13) lib/action_dispatch/routing/routes_proxy.rb:34:in `method_missing'
refinerycms-core (2.1.0) lib/refinery/admin/base_controller.rb:45:in `require_refinery_users!'
activesupport (3.2.13) lib/active_support/callbacks.rb:429:in `_run__280756230911749086__process_action__4059238740863068133__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
dragonfly (0.9.15) lib/dragonfly/cookie_monster.rb:9:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `_run__1258339801324610623__call__2555522013878269770__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/admin/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/admin/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/admin/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Request

Parameters:

{"locale"=>:en}
Show session dump

Show env dump

Response

Headers:

None

@raymondos
Copy link
Author

Hi @parndt ,do you have any suggestion about that?

@parndt
Copy link
Member

parndt commented Sep 22, 2013

You could override require_refinery_users! and modify the functionality to a path you control

@raymondos
Copy link
Author

Hi @parndt , I am confusing about I have override require_refinery_users! as below,why it's not working?

Add the following to config/initializers/refinery/user.rb
::Refinery::ApplicationController.module_eval do
  def just_installed?
    ::Role[:refinery].users.empty?
  end

  def refinery_user_required?
    if just_installed? and controller_name != 'users'
      redirect_to main_app.new_user_registration_path
    end
  end


  def store_location
    session[:return_to] = request.fullpath.sub("//", "/")
  end

  # Redirect to the URI stored by the most recent store_location call or
  # to the passed default.
  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  # This just defines the devise method for after sign in to support
  # extension namespace isolation...
  def after_sign_in_path_for(resource_or_scope)
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    home_path = "#{scope}_root_path"
    respond_to?(home_path, true) ? refinery.send(home_path) : refinery.admin_root_path
  end

  def after_sign_out_path_for(resource_or_scope)
    refinery.root_path
  end

  def refinery_user?
    refinery_user_signed_in? && current_refinery_user.has_role?(:refinery)
  end

  def refinery_user_signed_in?
    user_signed_in?
  end

  def authenticate_refinery_user!
    authenticate_user!
  end

  def current_refinery_user
    current_user
  end

  protected :store_location, :redirect_back_or_default, :refinery_user?

  def self.included(base)
    if base.respond_to? :helper_method
      base.send :helper_method, :current_refinery_user, :current_user_session,
                                :refinery_user_signed_in?, :refinery_user?
    end
  end

end

@raymondos
Copy link
Author

Another confusing is in my main app application_controller

def after_sign_in_path_for(resource_or_scope) 
   root_path
end

so when I override

def require_refinery_users!
   false
end

in controllers/refinery/admin_controller.rb
it will redirect to root_path and I can not see dashboard also.

@parndt
Copy link
Member

parndt commented Sep 23, 2013

You're not going to see the dashboard if you redirect to your application's root_path.. you probably want refinery.admin_root_path

@raymondos
Copy link
Author

Oops, I locate the problem is maybe refinery helper is not working, in dashboard page ,the error message is:

NoMethodError in Refinery/admin/dashboard#index

Showing /Users/admin/rails/app/views/refinery/_site_bar.html.erb where line #2 raised:

undefined method `admin?' for #<#<Class:0x007fed87a36ae8>:0x007fed87a35440>
Extracted source (around line #2):

1: <% if refinery_user? && "#{controller_name}##{action_name}" !~ %r{preview#show} %>
2:   <% unless admin? # all required JS included by backend. %>
3:     <% content_for :stylesheets, stylesheet_link_tag('refinery/site_bar') unless !!local_assigns[:exclude_css] %>
4:     <%= yield(:stylesheets) unless local_assigns[:head] || local_assigns[:exclude_css] %>
5:   <% end -%>

Seems admin? is a helper method and refinerycms helper is not working?

@raymondos
Copy link
Author

@parndt it says # all required JS included by backend. but seems not working :(

@raymondos
Copy link
Author

Ok, I found where the problem truly is, just wrote here for somebody who use the guide from refinerycms-rails-3-2-into-your-existing-app.
The code from the guide as below will cause lots of unexpected NoMethodError, do not use it!
@parndt thanks for your help and patience :)

::Refinery::ApplicationController.module_eval do
  def just_installed?
    ::Role[:refinery].users.empty?
  end

  def refinery_user_required?
    if just_installed? and controller_name != 'users'
      redirect_to main_app.new_user_registration_path
    end
  end


  def store_location
    session[:return_to] = request.fullpath.sub("//", "/")
  end

  # Redirect to the URI stored by the most recent store_location call or
  # to the passed default.
  def redirect_back_or_default(default)
    redirect_to(session[:return_to] || default)
    session[:return_to] = nil
  end

  # This just defines the devise method for after sign in to support
  # extension namespace isolation...
  def after_sign_in_path_for(resource_or_scope)
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    home_path = "#{scope}_root_path"
    respond_to?(home_path, true) ? refinery.send(home_path) : refinery.admin_root_path
  end

  def after_sign_out_path_for(resource_or_scope)
    refinery.root_path
  end

  def refinery_user?
    refinery_user_signed_in? && current_refinery_user.has_role?(:refinery)
  end

  def refinery_user_signed_in?
    user_signed_in?
  end

  def authenticate_refinery_user!
    authenticate_user!
  end

  def current_refinery_user
    current_user
  end

  protected :store_location, :redirect_back_or_default, :refinery_user?

  def self.included(base)
    if base.respond_to? :helper_method
      base.send :helper_method, :current_refinery_user, :current_user_session,
                                :refinery_user_signed_in?, :refinery_user?
    end
  end

end

@sunloverz
Copy link

@sky2127211 How you solved this problem?

@sdownie
Copy link
Contributor

sdownie commented Oct 5, 2013

@sky2127211 @sunloverz I apologize for taking so long to update my article for Refinery 2.1.

I've updated the article: http://sdownie.com/articles/refinerycms-rails-3-2-into-your-existing-app
Working example: https://github.com/sdownie/refinerycms-demo
Gist of the major fix: https://gist.github.com/sdownie/6843871.

@raymondos
Copy link
Author

@sunloverz Do not use user.rb file in the guides , override require_refinery_users! in refinery's admin_controller then it works! I saw the guide author updated the article, @sdownie thanks for your updating, I will try it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants