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

Routing issues #14

Closed
joeczucha opened this issue Apr 22, 2017 · 8 comments
Closed

Routing issues #14

joeczucha opened this issue Apr 22, 2017 · 8 comments

Comments

@joeczucha
Copy link

I'm trying to use connect Alchemy and Solidus but running into some routing errors.

Steps to reproduce:

rails _4.2.8_ new my_test_project --database=postgresql --skip-spring

Added to gemfile:

gem 'alchemy_cms', github: 'AlchemyCMS/alchemy_cms', branch: '3.5-stable'
gem 'alchemy-devise', github: 'AlchemyCMS/alchemy-devise', branch: '3.5-stable'

Then run:

bundle

bin/rake alchemy:install
bin/rails g alchemy:devise:install

(all working as expected so far)

Added to gemfile:

gem 'alchemy-solidus', github: 'AlchemyCMS/alchemy-solidus', branch: 'master'
gem 'solidus'

Then run:

bundle exec rails g spree:install --no-sample
bundle exec rails g spree:custom_user Alchemy::User

Configuration, as per docs:

# config/initializers/spree.rb
...
Spree.user_class = "Alchemy::User"
...

# lib/spree/authentication_helpers.rb
...
def spree_login_path
  alchemy.login_path
end

def spree_signup_path
  alchemy.signup_path
end

def spree_logout_path
  alchemy.logout_path
end
...

# config/routes.rb
root to: 'alchemy/pages#show'
mount Spree::Core::Engine, :at => '/'
mount Alchemy::Engine => '/'

Issues

Home route

Visiting http://localhost:3000 gives:

Routing Error
Alchemy::Page not found "/"

Resolved this by updating the routes.rb to include the homepage urlname:

root to: 'alchemy/pages#show', urlname: 'home'

Admin route

Visiting http://localhost:3000/admin hits the Spree routing layer and gives:

Authorization Failure

Resolved this by updating routes.rb, adding the following above the Spree mount:

scope :admin do
  get '/' => redirect("#{Alchemy.admin_path}/dashboard"), as: :admin
end

Users route

Both Alchemy and Solidus seem to use /admin/users routes. Clicking 'Users' takes me over to the Solidus dashboard, but adding a user through that gives the following error:

NoMethodError in Spree::Admin::Users#edit
Showing /Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/solidus_backend-1.4.0/app/views/spree/admin/users/edit.html.erb where line #33 raised:
undefined method `spree_api_key' for #Alchemy::User:0x007fc2396e68d0
Did you mean? spree_role_ids

I then tried changing routes.rb to have Spree mounted at '/store': mount Spree::Core::Engine, :at => '/store'

But visiting the /admin/users page in Alchemy then gives me the following:

An error happened

NoMethodError undefined method `firstname_or_lastname_or_login_or_email_or_gender_or_language_or_encrypted_password_or_password_salt_or_current_sign_in_ip_or_last_sign_in_ip_or_cached_tag_list_or_reset_password_token_or_alchemy_roles_cont' for #<Ransack::Search:0x007f7f
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/ransack-1.8.2/lib/ransack/search.rb:106:in `method_missing'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/ransack-1.8.2/lib/ransack/helpers/form_builder.rb:10:in `value'
...

So I'm a bit stuck now.

Thanks in advance for any tips!

@joeczucha
Copy link
Author

joeczucha commented Apr 22, 2017

Update: For anyone who stumbles across this issue: it seems that the bundle exec rails g spree:custom_user Alchemy::User creates a migration with a duplicate version number, which then silently fails to run. In my case it was:

/db/migrate/20170422161638_add_spree_fields_to_custom_user_table.rb
/db/migrate/20170422161638_create_spree_store_credit_payment_method.spree.rb

but obviously your version numbers will vary.

To resolve, simply rename /db/migrate/20170422161638_add_spree_fields_to_custom_user_table.rb so that it comes after all your other migrations and re-run rake db:migrate.

@joeczucha
Copy link
Author

Update: So, whilst this will now allow me to create users through the Spree dashboard, the users that it creates can't actually log in - probably because a lot of fields, such as login are missing.

I expect that it would be preferable to create the users through the Alchemy dashboard (rather than spree) so I've overridden the route as follows:

scope :admin do
  get '/' => redirect("#{Alchemy.admin_path}/dashboard"), as: :admin
  resources :users, controller: 'alchemy/admin/users'
end

This keeps me within Alchemy when I click the users button, as expected, but gives me the following rather spectacular error (!)

An error happened

NoMethodError undefined method `firstname_or_lastname_or_login_or_email_or_gender_or_language_or_encrypted_password_or_password_salt_or_current_sign_in_ip_or_last_sign_in_ip_or_cached_tag_list_or_reset_password_token_or_alchemy_roles_or_spree_api_key_cont' for #<Ransack
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/ransack-1.8.2/lib/ransack/search.rb:106:in `method_missing'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/ransack-1.8.2/lib/ransack/helpers/form_builder.rb:10:in `value'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/tags/base.rb:38:in `value_before_type_cast'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/tags/text_field.rb:13:in `block in render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/tags/text_field.rb:13:in `fetch'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/tags/text_field.rb:13:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/tags/search_field.rb:6:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/form_helper.rb:998:in `search_field'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/form_helper.rb:1323:in `search_field'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/bundler/gems/alchemy_cms-76142d94bf0e/app/views/alchemy/admin/partials/_search_form.html.erb:6:in `block in _26d50d91f5c04682aedd92676452833b'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:38:in `capture'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/form_helper.rb:444:in `form_for'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/ransack-1.8.2/lib/ransack/helpers/form_helper.rb:34:in `search_form_for'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/bundler/gems/alchemy_cms-76142d94bf0e/app/views/alchemy/admin/partials/_search_form.html.erb:3:in `_26d50d91f5c04682aedd92676452833b'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/template.rb:145:in `block in render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:166:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/template.rb:333:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/template.rb:143:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/deface-1.2.0/lib/deface/action_view_extensions.rb:41:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/partial_renderer.rb:309:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/renderer.rb:51:in `render_partial'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/rendering_helper.rb:35:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/bundler/gems/alchemy_cms-76142d94bf0e/app/helpers/alchemy/admin/base_helper.rb:317:in `block in toolbar'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:38:in `capture'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/helpers/capture_helper.rb:152:in `content_for'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/bundler/gems/alchemy_cms-76142d94bf0e/app/helpers/alchemy/admin/base_helper.rb:314:in `toolbar'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/bundler/gems/alchemy-devise-a892d279a883/app/views/alchemy/admin/users/index.html.erb:1:in `_4d8853c38fb82b4ac8f6b88092891b63'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/template.rb:145:in `block in render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:166:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/template.rb:333:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/template.rb:143:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/deface-1.2.0/lib/deface/action_view_extensions.rb:41:in `render'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
/Users/joe/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'

Ps. Sorry for the multiple posting, I'm just logging my findings here in case anyone else finds this useful.

@robinboening
Copy link
Contributor

robinboening commented May 6, 2017

Hi,

I set up a store and connected alchemy as well to reproduce your errors.

it seems that the bundle exec rails g spree:custom_user Alchemy::User creates a migration with a duplicate version number, which then silently fails to run.

I don't know what happened on your computer, but for me there was no issue running the generator. The following are the migrations created:

db/migrate/20170505214818_create_alchemy_users.alchemy_devise.rb
db/migrate/20170505214819_add_alchemy_roles_to_alchemy_users.alchemy_devise.rb
db/migrate/20170505214820_add_indexes_to_alchemy_users.alchemy_devise.rb
db/migrate/20170505214943_add_spree_fields_to_custom_user_table.rb

Visiting http://localhost:3000/admin hits the Spree routing layer and gives:
Authorization Failure

I got this, too. I looked into this issue and found that solidus raises CanCan::AccessDenied and rescues that with a redirect to /unauthorized. spree_auth_devise takes care of that and redirects to spree.admin_unauthorized_path but in your case you are using alchemy-devise which does not know anything about that. A solution is to implement the redirect in your app:

# /config/initializers/spree.rb
Spree::Admin::BaseController.unauthorized_redirect = -> do
  redirect_to spree_login_path
end

I noticed an error you did not mention or maybe did not face because you couldn't login: After you log in and open the storefront in the browser

undefined method `last_incomplete_spree_order' for #<Alchemy::User:0x007fa1d6c65410>

is thrown to your face. I investigated a moment and found that the Spree::UserMethods are needed in the User model. Sending these methods to the class fixes that. I added a new initializer:

# /config/initializers/alchemy_cms.rb
Alchemy::User.send :include, Spree::UserMethods

I haven't had this ransack error you mentioned when requesting admin/users. I also added the alchemy/admin/users route in the main app routes file, but it worked for me.

I hope my findings help you.

@joeczucha
Copy link
Author

joeczucha commented May 15, 2017

Hi @robinboening,

Thanks for your reply and for taking the time to test my findings. Apologies for the delay, I've been out of the country.

Could I please just confirm a few things about your test environment?

My routes file looks like this:

root to: 'alchemy/pages#show', urlname: 'home'
scope :admin do
  get '/' => redirect("#{Alchemy.admin_path}/dashboard"), as: :admin
  resources :users, controller: 'alchemy/admin/users'
end
mount Spree::Core::Engine => '/'
mount Alchemy::Engine => '/'

And my Gemfile is as follows:

source 'https://rubygems.org'
ruby '2.4.0'

* snip *

gem 'alchemy_cms', github: 'AlchemyCMS/alchemy_cms', branch: '3.5-stable'
gem 'alchemy-devise', github: 'AlchemyCMS/alchemy-devise', branch: '3.5-stable'
gem 'alchemy-solidus', github: 'AlchemyCMS/alchemy-solidus', branch: 'master'

gem 'solidus'
gem 'solidus_gateway'
gem 'deface'

Do these versions match yours?

I had included the Spree User methods (by means of a UserDecorator) but the result is still the same with the Ransack error message. As yours is working, are you able to add users successfully through the Alchemy dashboard?

Also, just so that I'm completely clear, have you opted to use alchemy-devise or solidus_auth_devise as your primary authentication method?

Perhaps you could push your version up on Github somewhere for reference ;)

Many thanks again for the advice,

Joe

@tvdeyen
Copy link
Member

tvdeyen commented Nov 17, 2017

The migration errors will be fixed once solidusio/solidus#2382 was merged.

@tvdeyen
Copy link
Member

tvdeyen commented Nov 17, 2017

Also I made setting up this gem more easy by adding a new generator in #16

@tvdeyen
Copy link
Member

tvdeyen commented Nov 17, 2017

@robinboening also fixed the missing Spree::UserMethods include in #16

@tvdeyen
Copy link
Member

tvdeyen commented Jan 12, 2018

These issues should all be solved now that we merged a bunch of PRs in Solidus and here. Please feel free to re-open if any issues are still present.

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

3 participants