Rails Routes: Nested Scopes/Namespaces - ruby-on-rails

Right now I have an admin.domain.com subdomain for which there is a module scope:
constraints(AdminDomain) do
scope :module => "admin" do
resources :visitors
end
end
This makes all requests on the admin subdomain hit controllers in app/controllers/admin/.
What I'd like to do now is something like:
constraints(AdminDomain) do
scope :module => "admin" do
resources :visitors
scope "history", :as => "history" do
resources :visitors
end
end
end
Where the end goal is to make admin.domain.com/history/visitors hit controller: app/controllers/admin/history/visitors_controller.rb.
This path however still looks for app/controllers/admin/visitors_controller.rb.
Any ideas?

namespace is what I was looking for:
constraints(AdminDomain) do
scope :module => "admin" do
resources :visitors
namespace :history do
resources :visitors
end
end
end
And I had to define Admin::History::VisitorsController in app/controllers/admin/history/visitors_controller.rb

easy way to define specific controller is just like:
:controller => admin/history/visitors

Related

How to save a Boolean entry in a has_many through join Table

I need to define a method/action in my LessonsController that I can call from the lesson show action that marks the lesson as being completed by the current user. What does that controller method look like?
Here's the overview of my models:
User
has_many :completions
has_many :completed_steps, through: :completions, source: :lesson
Lesson
has_many :completions
has_many :completed_by, through: :completions, source: :user
Completions
belongs_to :user
belongs_to :lesson
My Completions Table looks like this:
t.integer "user_id"
t.integer "lesson_id"
t.boolean "completed_step"
t.string "completed_by"
I'm assuming in the LessonsController it looks like this
def complete_step
self.completions.create(completed_step: true, user_id: current_user.id)
end
Routes info:
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :coupons
resources :lessons
resources :plans
resources :roles
resources :subscriptions
resources :completions
root to: "users#index"
end
devise_for :users, :controllers => { :registrations => "registrations"}
# Added by Koudoku.
mount Koudoku::Engine, at: 'koudoku'
scope module: 'koudoku' do
get 'pricing' => 'subscriptions#index', as: 'pricing'
end
resources :lessons do
member :complete
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#home'
get '/dashboard' => 'pages#dashboard', :as => 'dashboard'
mount StripeEvent::Engine, at: '/stripe-events' # prov
end
Here's my button link to make this functional.
<%= button_to "Mark this Lesson as Complete", complete_lesson_path(#lesson), method: :put, class: "btn btn-warning btn-lg" %>
Will this work or am I WAY off? Thanks!
Keep this is the LessonsController, but change it in either of the following ways:
def complete_step
current_user.completions.create(completed_step: true, lesson_id: #lesson.id)
end
# ~~ OR ~~
def complete_step
#lesson.completions.create(completed_step: true, user_id: current_user.id)
end
Both of these assume that you've already set #lesson in the controller, probably in a before_action :set_lesson.
EDIT:
If you need a route suggestion, then assuming you have resources :lessons in your routes file, you can either use an existing route (likely update) or add a member route like this:
resources :lessons do
get 'complete', on: :member
end
If you add a route, then you will need to add an action that looks like
def complete
complete_step
redirect #lesson
end
or similar, however you want to handle the response itself. You will also need to ensure that #lesson is set, so you should tweak your before_action :set_lesson, only: [:show, :update, ...] to also include :complete
Please try with below code.in your completion controller
def create
#lession = Lession.find(params[:lession_id])
#completion = current_user.completions.create(completed_step: true, lesson_id: #lesson.id)
redirected_to #completion
end
You can also just pass user: current_user to the completions.create method instead of passing in the current_user.id
Something like #lesson.completions.create(completed_step: true, user: current_user)

Put many resources and paths under subdomain (Rails Routes)

I'd like to forward a bunch of stuff to a subdomain in my rails applications (including nested resources), without going through all my views to update all the link_to _path links. Is this possible?
EDIT
So far I have the following. Problem is, I can access everything either with or without the subdomain, without being redirect once I enter or leave a resource that should be on the subdomain. E.g. example.com/apps is the same as connect.example.com/apps, and the site root can either be example.com or connect.example.com
scope '/' do
with_options :conditions => {:subdomain => 'connect'} do |site|
site.resources :contracts
site.resources :bills
site.resources :feedbacks
site.resources :newsletters
site.resources :contacts
site.resources :apps do
site.resources :elements, controller: 'apps/elements' do
site.resources :features, except: [:index], controller: 'apps/elements/features' do
member do
site.post 'complete'
end
end
end
site.resources :comments, controller: 'comments'
site.resources :bills, controller: 'bills'
site.resources :contracts, controller: 'contracts'
end
end
end
EDIT 2
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r307 %r{/apps/(.*)}, 'http://connect.localhost:3000/apps/$1'
end
https://github.com/jtrupiano/rack-rewrite will let you do just this. It's web server agnostic so it won't matter that you're on heroku.
eg.
config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
r307 %r{/apps/(.*)}, 'http://connect.example.com/apps/$1'
end
will issue a redirect for requests for /apps/ to go to connect.example.com/apps/

scope accessible rails 3.1

I have this scope:
scope ":user_id", :as => "user" do
resources :boards, :controller => 'users/boards'
end
I get this route:
http://localhost/hyperrjas/boards/
I want a url without boards then on routes.rb I add:
scope ":user_id", :as => "user" do
resources :boards, :controller => 'users/boards', :path => '/'
end
That works great, but it is still accessible via "/boards" ... How do I prevent that? (I'm using Rails 3.1)
You shouldn't have to specify the controller names when using resources and in this caseI would use nested resources:
resource :user, only: :show do
resources :boards
end
This should give you the following:
/:user_id
/:user_id/boards
/:user_id/boards/new
/:user_id/boards/:id/
/:user_id/boards/:id/edit
and of course your restful routes!

How to rewrite URLs except for an action using namespaces in Ruby on Rails?

I am running Ruby on Rails 3 and I would like to set up my routes in order to rewrite URLs using namespaces, except for an action (the index action).
In the routes.rb file I have:
namespace "users", :path => "user" do
resources :accounts
end
So, for example, URLs to "show"/"create new" accounts are:
http://<site_name>/user/accounts/1
http://<site_name>/user/accounts/new
I would like to rewrite/redirect those URLs, except for the 'index' action, as/to
# For the 'index' action I would like to use plural 'users' instead of 'user'
http://<site_name>/users/accounts
# and
http://<site_name>/users
How to do that?
I tryed this
namespace "users", :path => "user", :except => :index do
resources :accounts
end
but it doesn't work.
try this
namespace "users", :path => "user" do
resources :accounts, :except => :index
end

How do you add a custom route to a singleton resource?

map.resource :basket, :collection => { :checkout => :post }
The above does not work for a resource, as you would expect since basket is a resource (ie. singular) not resources, so there is no concept of a collection, everything should be scoped to the current_user. In this case User has_one Basket.
However I would like to specify a custom route without having to resort to adding another line in routes, eg:
map.checkout 'basket/checkout', :controller => 'baskets', :action => 'checkout'
Is this possible?
Of course my other option is to add a checkouts controller.
Just use :member option instead of :collection:
map.resource :basket, :member => {:checkout => :post}
If the basket is scoped to the user I'd make it a nested resource:
map.resources :users do |users|
users.resource :basket, :member => { :checkout => :post }
end
... or in Rails 3 ...
resources :users do
resource :basket do
post :checkout, :on => :member
end
end
This way you'll be able to scope the basket to the user who's checking out. The URL will end up looking like this:
/users/5/basket/checkout
You also get the nicely worded named route 'checkout_user_basket'.

Resources