Rails Subdomain for nested resources - ruby-on-rails

I've got a Rails 4 app that I'm trying to reconfigure to use subdomains for jobportals. Right now the root path for each portal is /jobportals/:id. What I want is for a user to be able to go to client.example.com and hit that root path. Then for example, if the user edits their profile, the url would be client.example.com/user/edit rather than www.example.com/jobportals/:id/user/edit.
I followed this ASCIIcasts tutorial on setting up subdomain routing, but it's not working for me. When I go to http://client.lvh.me:3000/, I am hitting the root_path of my Rails app, rather than the root_path for the portal.
I think the problem is that Constraints(Subdomain) isn't working with resources :jobportals. How can I reconfigure my routes to accomplish what I'm after?
MyApp::Application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
root 'general#home'
get '/about' => 'general#about'
get '/team' => 'general#team'
get '/careers' => 'general#careers'
get '/terms' => 'general#terms'
get '/privacy_policy' => 'general#privacy_policy'
constraints(Subdomain) do
resources :jobportals, controller: 'portals/general' do
member do
root 'portals/general#home'
devise_scope :user do
get '/user/sign_in' => 'portals/sessions#new'
post '/user/sign_in' => 'portals/sessions#create'
delete '/user/sign_out' => 'portals/sessions#destroy'
post '/user/password' => 'portals/passwords#create'
get '/user/password/new' => 'portals/passwords#new'
get '/user/password/edit' => 'portals/passwords#edit'
patch '/user/password' => 'portals/passwords#update'
put '/user/password' => 'portals/passwords#update'
post '/user' => 'portals/registrations#create'
get '/user/sign_up' => 'portals/registrations#new'
get '/user/edit' => 'portals/registrations#edit'
patch '/user' => 'portals/registrations#update'
put '/user' => 'portals/registrations#update'
delete '/user' => 'portals/registrations#destroy'
end
get '/jobs' => 'portals/general#jobs'
get '/companies' => 'portals/general#companies'
get '/alljobs' => 'portals/general#alljobs'
resources :applications, controller: 'portals/applications'
get ':id' => 'portals/companies#profile'
get ':id/jobs' => 'portals/companies#jobs'
get ':id/jobfunctions' => 'portals/companies#jobfunctions'
end
end
end
end
Working Code Below
MyApp::Application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
root 'general#home'
get '/about' => 'general#about'
get '/team' => 'general#team'
get '/careers' => 'general#careers'
get '/terms' => 'general#terms'
get '/privacy_policy' => 'general#privacy_policy'
constraints(Subdomain) do
get '/' => 'portals/general#home'
devise_scope :user do
get '/user/sign_in' => 'portals/sessions#new'
post '/user/sign_in' => 'portals/sessions#create'
delete '/user/sign_out' => 'portals/sessions#destroy'
post '/user/password' => 'portals/passwords#create'
get '/user/password/new' => 'portals/passwords#new'
get '/user/password/edit' => 'portals/passwords#edit'
patch '/user/password' => 'portals/passwords#update'
put '/user/password' => 'portals/passwords#update'
post '/user' => 'portals/registrations#create'
get '/user/sign_up' => 'portals/registrations#new'
get '/user/edit' => 'portals/registrations#edit'
patch '/user' => 'portals/registrations#update'
put '/user' => 'portals/registrations#update'
delete '/user' => 'portals/registrations#destroy'
end
get '/jobs' => 'portals/general#jobs'
get '/companies' => 'portals/general#companies'
get '/alljobs' => 'portals/general#alljobs'
resources :applications, controller: 'portals/applications'
get ':id' => 'portals/companies#profile'
get ':id/jobs' => 'portals/companies#jobs'
get ':id/jobfunctions' => 'portals/companies#jobfunctions'
end
end

It is looking like constraints are not define properly, it is something like get 'jobportals', constraints: {subdomain: 'subdomain_name'}
Checkout the rails guide

Related

How to add ScriptTag on shopify_api gem?

Hi Everyone,
I came across on a problem that I can't really figure out myself, It believe that all your expertise can help me through solving this error I get whenever I try to access a route to add a script. Here is my controller code:
class HomeController < AuthenticatedController
def index
#products = ShopifyAPI::Product.find(:all, :params => {:product_type => "Underarmour"})
# script = ShopifyAPI::ScriptTag.new(:all, :params => {:event => "onload", :src => "https://shopperapproved.herokuapp.com/sajs/14043.js"})
end
def script
ShopifyAPI::ScriptTag.create(:event => "onload", :src => "https://shopperapproved.herokuapp.com/sajs/14043.js")
end
end
and my route file is:
controller :sessions do
get 'login' => :new, :as => :login
post 'login' => :create, :as => :authenticate
get 'auth/shopify/callback' => :callback
get 'logout' => :destroy, :as => :logout
end
root :to => 'home#index'
match "script/",
:to => "home#script",
:via => :get
I want to add a script by accessing this route: on my index view:
<h3>Add your ShopperApproved site ID:</h3>
https://shopperapproved.herokuapp.com/script --> if i am going to click this link i will be redirected to HomeController#script
I hope you can help me..
Have you set up a seesion & token? i fnot you cant create the script in your store
https://github.com/Shopify/shopify_api
I assume you did not set up a session so you cant "connect" to your store.
Verify that you have a valide session and it will work

Hot to redirect "example.com/////" to just "example.com" in Rails app?

My goal is when user requests root (AND ONLY ROOT, other pages work fine) with something like:
http://domain.com/////
http://domain.com//////////////
http://domain.com//
app should 301 redirect user to one and only correct:
http://domain.com/
I'm using Webrick, rack-rewrite gem and planning to host it on Heroku.
Please remember that i'm new in Rails.
UPDATE ======
Rails: redirect all unknown routes to root_url
— this is totally different problem. They just redirect all unknown hosts to root. I have 404 working normally.
My problem is that any amount of slashes works as homepage and URL has all of those slashes. I'm doing it for SEO to get rid of duplicates of homepage. I want it to work like here: http://netpeak.ua (try "netpeak.ua////////", you will be redirected to "netpeak.ua").
UPDATE 2 - added content of routes.rb ======
Rails.application.routes.draw do
resources :questions
resources :feedbacks
devise_for :users
get '' => 'public#index'
get '/agency' => 'public#about'
get '/contact' => 'public#contact'
get '/uslugi' => 'services#index'
get '/portfolio' => 'projects#index'
get '/uslugi/:id' => 'services#show'
get '/portfolio/:id' => 'projects#show'
resources :articles, param: :id
resources :settings, as: 'home' #home so it doesn't go to another page
namespace :admin do
resources :articles, :users, :projects, :services, :feedbacks, :questions
get '' => 'projects#index'
get 'contact' => 'settings#contact'
get 'feedback' => 'settings#feedback'
get 'fininfo' => 'settings#fininfo'
get 'calls' => 'settings#calls'
get 'orders' => 'settings#orders'
get 'letters' => 'settings#letters'
get 'allquestions' => 'settings#allquestions'
get 'projectsettings' => 'settings#projectsettings'
get 'servicessettings' => 'settings#servicessettings'
get 'aboutsettings' => 'settings#aboutsettings'
get 'startpagesettings' => 'settings#startpagesettings'
patch 'contact' => 'settings#update'
patch 'feedback' => 'settings#update'
patch 'fininfo' => 'settings#update'
patch 'projectsettings' => 'settings#update'
patch 'servicessettings' => 'settings#update'
patch 'aboutsettings' => 'settings#update'
patch 'startpagesettings' => 'settings#update'
end
end

Rails action controller routing error

I try to add an administration area to my website. So I added a namespace that contains the controllers "posts" and "category".
In the routes.rb file, I added
namespace :admin do
resources :posts , :categories
match '/' => 'posts#index', :via => :get
end
but with the url:
myurl.com/admin
i have error:
No route matches {:controller=>"admin/site", :action=>"home", :locale=>:fr}
It seems that another route is called and overwrites the one I added
but in rake routes i have:
admin GET /admin(.:format) admin/posts#index
Here the complete rake routes:
faq GET (/:locale)/faq(.:format) site#faq {:locale=>/[a-zA-Z]{2}/}
rules GET (/:locale)/rules(.:format) site#rules {:locale=>/[a-zA-Z]{2}/}
advanced_rules GET (/:locale)/advanced_rules(.:format) site#advanced_rules {:locale=>/[a-zA-Z]{2}/}
play_now GET (/:locale)/play_now(.:format) play#play {:locale=>/[a-zA-Z]{2}/}
play GET (/:locale)/play(.:format) site#play {:locale=>/[a-zA-Z]{2}/}
home GET (/:locale)/home(.:format) site#home {:locale=>/[a-zA-Z]{2}/}
GET (/:locale)/qrcode(/:code)(.:format) site#qrcode {:locale=>/[a-zA-Z]{2}/}
forgot_password GET (/:locale)/forgot_password/:id(.:format) passwords#forgot_password {:locale=>/[a-zA-Z]{2}/}
android_redirect GET (/:locale)/android_redirect(.:format) android#index {:locale=>/[a-zA-Z]{2}/}
general_conditions GET (/:locale)/general_conditions(.:format) site#general_conditions {:locale=>/[a-zA-Z]{2}/}
support GET (/:locale)/support(.:format) site#support {:locale=>/[a-zA-Z]{2}/}
GET (/:locale)/invitation(/:source)(.:format) invitation#redirection {:locale=>/[a-zA-Z]{2}/}
release_notes GET (/:locale)/release_notes(.:format) site#release_notes {:locale=>/[a-zA-Z]{2}/}
release_note GET (/:locale)/release_notes/:version(.:format) release_notes#show {:locale=>/[a-zA-Z]{2}/}
root GET /(:locale)(.:format) site#home {:locale=>/[a-zA-Z]{2}/}
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_admin_category GET /admin/categories/new(.:format) admin/categories#new
edit_admin_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
admin_category GET /admin/categories/:id(.:format) admin/categories#show
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
admin GET /admin(.:format) admin/posts#index
completed GET /payment_completed(.:format) payment#completed
android GET /android(.:format) hockeyapps#android
login POST /login(.:format) registrations#login
loginfacebook POST /loginfacebook(.:format) registrations#loginfacebook
status GET /status(.:format) registrations#status
logout DELETE /logout(.:format) registrations#logout
geturl POST /geturl/:id/:token(.:format) paypals#geturl
getstatus POST /getstatus/:id/:token(.:format) paypals#getstatus
payment_completed GET /payment_completed(.:format) payment#completed
forgot_password GET /forgot_password/:id(.:format) passwords#forgot_password
send_password POST /send_password(.:format) passwords#send_password
Here the complete file routes.rb:
scope "(:locale)", :constraints => {:locale => /[a-zA-Z]{2}/} do
match '/faq' => 'site#faq', :via => :get
match '/rules' => 'site#rules', :via => :get
match '/advanced_rules' => 'site#advanced_rules', :via => :get
match '/play_now' => 'play#play', :via => :get
match '/play' => 'site#play', :via => :get
match '/home' => 'site#home', :via => :get
match '/qrcode/(:code)' => 'site#qrcode', :via => :get
match '/forgot_password/:id' => 'passwords#forgot_password', :via => :get, :as => "forgot_password"
match '/android_redirect' => 'android#index', :via => :get, :as => "android_redirect"
match '/general_conditions' => "site#general_conditions", :via => :get
match '/support' => "site#support", :via => :get
match '/invitation/(:source)' => 'invitation#redirection', :via => :get
match '/release_notes' => 'site#release_notes', :via => :get, :as => "release_notes"
match '/release_notes/:version' => 'release_notes#show', :via => :get, :as => "release_note"
root :to => 'site#home', :via => :get
end
namespace :admin do
resources :posts , :categories
match '/' => 'posts#index', :via => :get
end
get 'payment_completed' => 'payment#completed', :as => "completed"
get 'android' => 'hockeyapps#android', :as => 'android'
post 'login' => 'registrations#login', :as => 'login'
post 'loginfacebook' => 'registrations#loginfacebook', :as => 'loginfacebook'
get 'status' => 'registrations#status', :as => 'status'
delete 'logout' => 'registrations#logout', :as => 'logout'
post 'geturl/:id/:token' => 'paypals#geturl', :as => 'geturl'
post 'getstatus/:id/:token' => 'paypals#getstatus', :as => 'getstatus'
get 'payment_completed' => 'payment#completed', :as => 'payment_completed'
#scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
# resources :passwords
#end
get 'forgot_password/:id' => 'passwords#forgot_password', :as => 'forgot_password'
post 'send_password' => 'passwords#send_password', :as => 'send_password'
end
Any idea about the problem ?
thanks
match '/' is handled by the root :to => ... and namespaced. So routing tries to call site#home in module (namespace) admin.
Try to put the namespace part in front of the root :to or match '/admin' separately before defining root
Try this
namespace :admin do
resources :posts , :categories
get '', to: 'posts#index', as: '/'
end
scope "(:locale)", :constraints => {:locale => /[a-zA-Z]{2}/} do
...
root :to => 'site#home', :via => :get
...
end
Output of rake routes
admin GET /admin(.:format) admin/posts#index
Tried with get localhost:3000/admin
Started GET "/admin" for 127.0.0.1 at 2013-06-11 17:41:40 +0700
Processing by Admin::PostsController#index as HTML
Rendered admin/posts/index.html.erb within layouts/application (1.0ms)
Completed 200 OK in 1095ms (Views: 1095.1ms | ActiveRecord: 0.0ms)

Custom POST routes for create action not fired up

# Explaining the context
puts "I am learning Rails, building a simple forum application."
puts "I am pretty satisfied to where I got so far but routes... "
puts "...still figuring them out."
puts "Been 2 days trying all sorts of things."
puts "This is where I am now, and something is not working as expected."
puts "Any help/pointers would be appreciated! :)"
# The Problem
puts "I want my forum's create path to be '/helpcenter' and not '/helpcenter/cat'."
puts "When I access the form to create a new forum and I hit submit, "
puts "the form post to '/helpcenter' correctly (firebuged)"
puts "but I get the index, not the create!"
puts "I even put debugger in my create action but it is not being called."
# config/routes.rb
scope "/helpcenter" do
resources :cat, :controller => "forums", :as => :forums do
resources :topics , :controller => "forum_topics", :as => :topics
resources :posts, :controller => "forum_posts", :as => :posts
end
end
match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
I hope this problem is in the way I created the route. I tried many different things.
# _form.html.erb
<%= form_for(#forum) do |f| %>
....
<% end %>
I am using standard form_for helper.
# Rake Routes for Forums
$ CONTROLLER=forums rake routes
delete_forum GET /helpcenter/cat/:id/delete(.:format) forums#delete
forums GET /helpcenter/cat(.:format) forums#index
POST /helpcenter/cat(.:format) forums#create
new_forum GET /helpcenter/cat/new(.:format) forums#new
edit_forum GET /helpcenter/cat/:id/edit(.:format) forums#edit
forum GET /helpcenter/cat/:id(.:format) forums#show
PUT /helpcenter/cat/:id(.:format) forums#update
DELETE /helpcenter/cat/:id(.:format) forums#destroy
forums /helpcenter(.:format) forums#index
create_forum POST /helpcenter(.:format) forums#create
We clearly see a route for POST /helpcenter which is binded to the create action of the forums controller.
# Logs
Started POST "/helpcenter" for 127.0.0.1 at 2012-07-02 12:25:00 -0400
Processing by ForumsController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"d5iVKCh234234=", "forum"=>{"name"=>"", "description"=>""}, "commit"=>"Save Changes"}
The logs clearly shows I am doing a POST on /helpcenter but that it fires up the index action instead of the create action!
# What am I doing wrong?
puts "Thanks!"
I think the request matches your first forums route since you didn't specify a HTTP method. This should work:
match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
Or the shorthand version:
get "/helpcenter" => "forums#index", :as => :forums
post "/helpcenter" => "forums#create", :as => :create_forum
First glance shows that a POST against /helpcenter passes the rule for the forums#index match, which is encountered first, so that's what you get
match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
What about:
match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum

Rails 3 | Routing : How to rename resource title now?

Generally I have MenuItems model and trying to make '/menu_items(/:id(:/some_action))' URLs looks like '/menu(/:id(:/some_action))'
In Rails 2.3.5 it was
map.resources :menu_items, :as => :menu, :path_names => { :new => 'add' }
Now in Rails 3.0.3 I'm able to handle it using this huge paragraph of code
resources :menu_items, :path_names => { :new => 'add' }
match 'menu/' => 'menu_items#index', :as => :menu
match 'menu/add' => 'menu_items#new', :as => :new_menu
match 'menu/:id' => 'menu_items#show', :as => :show_menu
match 'menu/:id/edit' => 'menu_items#edit', :as => :edit_menu
But it looks incorrect because of huge amount of code.
Seems :as works like 2nd Rails' map.some_name now.
Any help/suggestions/guides? (Thanks)
http://guides.rubyonrails.org/routing.html#customizing-resourceful-routes
resources :menu, :controller => "menu_items", :path_names => { :new => "add" }
Output is quite close to what you're after:
menu_index GET /menu(.:format) {:controller=>"menu_items", :action=>"index"}
POST /menu(.:format) {:controller=>"menu_items", :action=>"create"}
new_menu GET /menu/add(.:format) {:controller=>"menu_items", :action=>"new"}
edit_menu GET /menu/:id/edit(.:format) {:controller=>"menu_items", :action=>"edit"}
menu GET /menu/:id(.:format) {:controller=>"menu_items", :action=>"show"}
PUT /menu/:id(.:format) {:controller=>"menu_items", :action=>"update"}
DELETE /menu/:id(.:format) {:controller=>"menu_items", :action=>"destroy"}
Another way
resources :menu, :as => :menu_items, :controller => :menu_items
This approach has the advantage that the exiting helper methods such as menu_items_path still work.

Resources