Rails Domain Constraints ( to serve multiple domains ) - ruby-on-rails-4

$ rails -v Rails 4.2.1
$ ruby -v ruby 2.2.2p95 (2015-04-13 revision > 50295) [x86_64-linux]
I am building an API for a mobile app, which will have an admin interface to it. What i'm attempting to do, is run this through nginx using unicorn ( which I have running on my dev environment)
I have 2 domains routed to the exact same rails project. These domains are: api.project.dev and admin.api.project.dev
I've read this:
http://guides.rubyonrails.org/routing.html#advanced-constraints
and tried:
Separate Domain for Namespaced Routes in Rails 4 ( see answer )
I've tried a few other things to try and get this to work, the only thing that comes up ( for either sub-domain ) is:
Invalid route name, already in use: 'root'
My current implementation of this is:
class DomainConstraint
def initialize(domain)
#domains = domain
end
def matches?(request)
#domains.include? request.domain
end
end
and
require 'domain_constraint'
Rails.application.routes.draw do
resources :statuses
constraints (DomainConstraint.new('api.project.dev')) do
root :to => 'statuses#index'
end
constraints(DomainConstraint.new('admin.api.project.dev')) do
root :to => 'statuses#new'
end
end
keep in mind that the roots are different pages only for now, but ultimately will be completely different systems.
Not quite sure where to go from here in order to get this functioning as I would hope.

With the fine help of the great people in #RubyOnRails on irc I got this figured out. So thanks crankharder and sevenseacat for your input and advice.
What I ended up with was this:
class DomainConstraint
def initialize(domain)
#domains = domain
end
def matches?(request)
#domains.include? request.host
end
end
and:
require 'domain_constraint'
Rails.application.routes.draw do
constraints DomainConstraint.new('api.project.dev') do
resources :statuses
root :to => 'statuses#index', as: 'api_root'
end
constraints DomainConstraint.new('admin.api.project.dev') do
resources :statuses
root :to => 'statuses#new'
end
end

You can also constrain a route based on any method on the Request object that returns a String. http://guides.rubyonrails.org/routing.html#request-based-constraints
The methods available to Request include host, which could be used as follows:
constraints host: 'api.project.dev' do
resources :statuses
root to: 'statuses#index', as: 'api_root'
end
constraints host: 'admin.api.project.dev' do
resources :statuses
root to: 'statuses#new'
end

Related

Customize resource routing in Rails 4

think of the resource routing of a photo-class for example.
If I this to my routes.rb I will get following routes:
resources :photos
# GET '/photos/', :to => 'photos#index'
# GET '/photos/:photo_id/, :to => 'photos#show'
# and so on and so on
Now what I want is to replace the word /photos in all the routes with a simple /p so that I can get a short URL like /p/1 for the first photo. Is there a way to simply alter the resource-line or do I have to manually add each route?
This will make all your routes via :photos through p
resources :p, :controller => "photos"
To be more concise and avoid the issue with p_id, you could do it like this :
resources :photos, path: 'p'
This way, you benefits from the readibility on your end (it will generate helpers like edit_photo_path, you will access variables as photo_id in case of a nested route and such) and generate the named URLs you do want.

Ruby on Rails App Testing with Rspec and Capybara

when i create a feature test for my application get the following error:
ActionController::RoutingError:
No route matches [GET] "/example"
My application uses subdomains and sub-applications(engines/modules) within these subdomains. Now when i set for Capybara the app_host or default_host through an feature_subdomain_helper like
Capybara.app_host = "example.lvh.me" or
Capybara.default_host = "example.lvh.me"
and into my rails_helper.rb i add the following code line
config.extend SubdomainHelpers, type: :feature
I get the same error. Now i think the configured subdomain are not considered by my feature test.
My Rspec Version is: 3.2
and Capybara Version is: 2.4.4
My sample feature test looks like:
require 'rails_helper'
feature 'Example Page' do
scenario 'visit example page' do
visit "/example"
expect(page).to have_content 'Example'
end
end
Have someone an idea what i do wrong?
Edit:
Mainapp routes:
constraints(Subdomain) do
mount Example::Engine => '/', as: 'example'
end
Engine routes:
Example::Engine.routes.draw do
scope '/example', nav_scope: 'example' do
end
end
The names of Capybara.default_host and Capybara.app_host are slightly misleading since they both need to be set as URLs to function properly
Capybara.default_host = "http://example.lvh.me"
If that doesn't fix your issue check rake routes and make sure the action you think is mounted at '/example' really is.

No route matches {:action=>"show" ... missing required keys: [:id]

Seeing many question related to this but none of them gives answer to my problem.
I have Rails api application without ActiveRecord support. It is easy to reproduce problem. Follow steps:
Create rails api site without ActiveRecord support
rails new test001 -O --api
Change folder to test001 and run:
rails g scaffold testapp id name
Create model file testapp.rb in app/models folder
class Testapp
include ActiveModel::Model
attr_accessor :id, :name, :created_at, :updated_at
def self.all
t1 = Testapp.new(:id =>111, name: "t111")
return [t1]
end
end
Start server
rails s
Using postman REST client create GET request
http://localhost:3000/testapps.json
It fails with error ActionView::Template::Error (No route matches {:action=>"show", :controller=>"testapps", :format=>:json, :id=>#<Testapp:0x00000005411518 #id=111, #name="t111">} missing required keys: [:id]):
I have dummy implementation for POST, PUT, GET 1 item and all works. Here is dummy implementation of GET 1 item (/testapps/x.json)
def self.find(p)
return Testapp.new(:id =>p, name: "t123")
end
What is the problem with GET all (/testapps.json)?
Found solution.
Problem is scaffold generated index.json.jbuilder
file:
json.array!(#testapps) do |testapp|
json.extract! testapp, :id, :id, :name
json.url testapp_url(testapp, format: :json) #REMOVE
end
It added line json.url testapp_url(testapp, format: :json) for no reason. json.extract! deserialized object already.
Removing line solved problem.
I still do not know why testapp_url(testapp, format:json) caused error. Checking Rails Routing document http://guides.rubyonrails.org/routing.html

Redirecting a request in a routing constraint

I have Sidekiq mounted in my routes file to the /sidekiq endpoint.
I use a constraints option to have it call an external class for validation as a way of preventing non-privelaged users from accessing that endpoint.
# config/routes.rb
mount Sidekiq::Web => "/sidekiq", constraints: Sidekiq::AdminConstraint.new
# lib/sidekiq/admin_constraint.rb
module Sidekiq
class AdminConstraint
def matches?(request)
return false unless request.session[:user_id]
user = User.find_by_id(request.session[:user_id])
user && Ability.new(user).can?(:manage, :sidekiq)
end
end
end
This setup works great. However, it only lets me return true / false on whether the request should go through or not. It does not let me -
Set a flash message (e.g. "You are not permitted to access that page") and
Redirect to some arbitrary page
In that sense, I'm looking for it to behave more like a controller's before_filter.
Is there a way I can modify the request object that's passed in to implement that redirect?
Thanks!
I don't have idea directly set the flash messages, But we can use in different way.
Use the following solution
In your routes.rb, add the following line in the end of the file
match "*path", :to => "application#error_404"
This basically means, any path that is not defined in your route will end up going to error_404 in application_controller. Its very important to put this at the end of your file
And in your ApplicationController, add
def error_404
redirect_to root_path
end
Thanks

uninitialized constant Api::Doorkeeper

I have API engine inside my Rails app and I've mounted the engine under in the main app routes
Rails.application.routes.draw do
mount Api::Engine => "/api"
end
and I want to add the doorkeeper routes using use_doorkeeper function in my routes like this
Api::Engine.routes.draw do
use_doorkeeper :scope => "api/oauth"
end
this doesn't work because it tries to find controllers under api/doorkeeper/controller_name instead of doorkeeper/controller_name
as a workaround I've added doorkeeper routes in main app routes.rb with a scope like this
Rails.application.routes.draw do
mount Api::Engine => "/api"
use_doorkeeper :scope => "api/oauth"
end
But I want to know if there is a solution so I can still add the routes to api/config/routes.rb and make reference the correct controllers path
my colleague suggested this solution found here and it worked for me :)
MyEngine::Engine.routes.draw do
old_scope = #scope[:module]
#scope[:module] = nil
use_doorkeeper
#scope[:module] = old_scope
end