Adding Routes to Rails' Spree E-Commerce - ruby-on-rails

I just recently upgraded to 1.0.3, and the routes.rb file in my config/routes folder seems to ignore all my custom routes.
MY routes.rb
JollyStore::Application.routes.draw do
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'
root :to => 'pages#index'
namespace :admin do
resources :wysiwygs
end
match 'about_us/', :to => "pages#about_us"
match 'services/', :to => "pages#services"
match 'raw_resources/', :to => "pages#raw_resources"
match 'contact_us/', :to => "pages#contact_us"
match 'privacy_policy/', :to => "pages#privacy_policy"
match 'return_policy/', :to => "pages#return_policy"
match 'refund_policy/', :to => "pages#refund_policy"
match 'cancellation_policy/', :to => "pages#cancellation_policy"
match 'delivery_shipping_policy/', :to => "pages#delivery_shipping_policy"
end
If I run bundle exec rake routes, it returns all the approriate routes. But when I try to reach that specific page, I get :
undefined local variable or method `about_us_path'
Or the same error for every link that is within my custom routes. Somehow my routes are being ignored. Does anyone know a way to circumvent this issue?

I encountered the same error and found this solution, which solved it by prefixing main_app, before each of my_paths/_urls. In my case, these were links used in one of the /override.rb files.
So, try: main_app.about_us_path.

You can add new routes in the Spree using following block in routes.rb file
Spree::Core::Engine.routes.prepend do
# Your new routes
end

For me prepend did not work.
for me draw did the work:
Spree::Core::Engine.routes.draw do
resources :orders, except: [:new, :create, :destroy] do
post :my_order, on: :collection
end
end

Related

Rails "redirect_to" Nested routes with latest model

In my rails app I have two models nested,
Gameround > currentplayer
Gameround is shown on play.html.erb, I also make the currentplayers there. When the currentplayer is made I want to redirect the user to currentplayer#show but I can't seem to figure out the way to route the link. I've tried everything I can think of.
So I need a link that says:
Get url to thiscurrentGameround/ThiscurrentplayerIjustmade
My controller:
def createPlayerforUser
#latest_game_round = Gameround.order(created_at: :desc).first
#currentplayer = #latest_game_round.currentplayers.create({
log_id: #current_user.id
});
if #currentplayer.save
redirect_to url_for([#gameround, #currentplayer])
end
end
config.routes
resources :gamerounds do
resources :currentplayers
end
resources :gamesessions
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# Artikkel, Alien liste
resources :expansions do
resources :aliens
end
resources :users
# You can have the root of your site routed with "root"
#root 'gamesessions#play'
root 'gamesessions#new'
#root 'application#show'
#root 'public#index'
get "signup", :to => "users#new"
get "login", :to => "sessions#login"
post "login_attempt", :to => "sessions#login_attempt"
get "logout", :to => "sessions#logout"
get "profile", :to => "sessions#profile"
get "setting", :to => "sessions#setting"
get "play", :to => "gamesessions#play"
get "wait", :to => "gamesessions#wait"
get "aliens", :to => "aliens#index"
If you run rake routes in the terminal, your list of routes should include one for showing currentplayer. You should be able to use
redirect_to gameround_currentplayer_url(#latest_game_round, #current_player)

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Routes working fine on local but not in Production

I am currently trying to get my Ruby on Rails application running in a production environment. It is working fine but when I added a route under a nested namespace it is giving me error saying
ActionController::RoutingError (uninitialized constant Agent::Clients::AccountController)
This routes are working fine on my local machine, The route looks like this
namespace :agent do
root :to => redirect('url')
match 'dashboard', :to => 'dashboard#index'
match 'account', :to => 'account#edit'
match 'account/update', :to => 'account#update'
namespace :clients do
root :to => redirect('url')
**# This part I added and is giving routing error**
match 'accounts/invite', :to => 'clients/account#invite'
match 'accounts/sendinvite', :to => 'clients/account#send_invitation'
My rake routes giving the routes properly.
Any suggestions how to fix this issue ?
So you either want to do
match 'accounts/invite', :to => 'clients/accounts#invite'
match 'accounts/sendinvite', :to => 'clients/accounts#send_invitation'
Or
match 'accounts/invite', :to => 'agent/account#invite'
match 'accounts/sendinvite', :to => 'agent/account#send_invitation'

Rails Path Helper for Root should output "/"

In my routes file, I have a resource called products. The index action of the products resource is also my root path.
resources :products
root :to => "products#index"
When I use the helper method products_path (in a redirect or link), it returns "/products". But what I want is for it to return "/". I know it's the same page, but I want to keep my URL's consistent.
How can I fix this?
Thanks!
root :to => 'products#index', :as => :products
match '', :to => 'products#index', :as => :root # recreate named root_path, if you use it anywhere
This will need to appear below your resources :products as it does in your example above. This will override the products_path and products_url you'd get from resources. Run rake routes before and after to compare.
if you only want to change Index try excluding it first then defining it on its own like so:
resources :products, :except => [:index]
resources :products, :only => [:index], :path => '/'
root_path() should return /
I guess you could redefine the products_path to be the same as root_path (like in a helper file):
def products_path(*params)
root_path *params
end

Typus route order

It used to be that you could load Typus routes exactly where you needed them by placing
Typus::Routes.draw(map)
at the appropriate point in your routes.rb file. It seems that this is no longer supported and that they're always loaded after all of the application routes. This causes problems with catchall routes which must be defined last. Does anyone know how to control the load order for typus now? Is there a way to get them defined before any of the app routes rather than after? Thanks!
I got around it by leaving my catch-all routes at the end of my apps routes.rb BUT excluding it from matching for Typus urls:
# A catch all route
match '*path' => 'content#show', :constraints => lambda{|request|
!request.path.starts_with?("/admin") # excluded if typus will be taking it...
}
This may or may now work for you...
I'm looking for the same answer.
At the moment I have resorted to copying the contents from typus's config/routes.rb and placing it into my routes.rb file, before the catchall route.
It's a horrible, hackish solution, but it's solving my immediate problem.
Example:
# TODO: KLUDGE: MANUALLY BRING THE TYPUS ROUTES IN
# Typus used to provide :
# Typus::Routes.draw(map)
# But that is no longer the case.
scope "admin", :module => :admin, :as => "admin" do
match "/" => "dashboard#show", :as => "dashboard"
match "user_guide" => "base#user_guide"
if Typus.authentication == :session
resource :session, :only => [:new, :create, :destroy], :controller => :session
resources :account, :only => [:new, :create, :show, :forgot_password] do
collection do
get :forgot_password
post :send_password
end
end
end
Typus.models.map { |i| i.to_resource }.each do |resource|
match "#{resource}(/:action(/:id(.:format)))", :controller => resource
end
Typus.resources.map { |i| i.underscore }.each do |resource|
match "#{resource}(/:action(/:id(.:format)))", :controller => resource
end
end
# END KLUDGE
# Catch all to the state page handler
match '/:page' => 'pages#show', :as => 'page'

Resources