Rails 4应用程序中的子域 [英] Subdomains in a Rails 4 app
问题描述
此外,现在我正在使用WEBrick,即使这不是我在生产中使用的,而我使用域lvh.me访问子域。
感谢您的帮助。
编辑:这是我的routes.rb文件:
Rails。 application.routes.draw do
devise_for:users
root'static_page#index'
end
我遵循Devise wiki中的这个指南:
https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain
所以我通过迁移删除了电子邮件的唯一性,接下来我补充说:subdomain request_keys如下:
devise:database_authenticatable,:recoverable,:rememberable,,trackable,:timeoutable,request_keys:[:subdomain]
然后,我继续覆盖find_for_authentication功能,如下所示:
def self.find_for_authentication (conditions = {})
条件[:account_id] = Account.find_by_su bdomain(conditions.delete(:subdomain))。id
super(conditions)
end
这只是总结一下。
编辑:
我一直在搞乱,我发现了这个问题。 根指令将所有子域指向我的域。所以如果我从我的routes.rb中删除root,每个子域都会导致一个RouteError。我记得在我的Devise应用程序中需要一个根目录。所以我不知道这是Devise的行为还是根本的。
我手动解决了。我在routes.rb中找到了root指令,接受来自每个子域的请求。最后,我实现了一个自定义约束,并将其添加到routes.rb文件中devise_for的约束。这充分解决了我的问题。感谢大家的帮助!
更新:
为了回应我的代码的意见:我们有一个系统设置,每个帐户(业务帐户)将有自己的子域,每个业务帐户将有很多用户。我的子域约束看起来像
lib / subdomain.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? &安培;&安培; Account.exists?(subdomain:request.subdomain)
end
end
然后在我的config / routes.rb中,我使用这个约束来路由请求:
config / routes.rb
require'Subdomain'
Rails.application.routes.draw do
#有些路由...
constraints(Subdomain)do
devise_for:users,controllers:{:sessions => 'session'}
root'dashboard#index'
#更多子域限制路由...
end
end
在我发布的原始问题中,我已经显示了我的其他设置。我希望这有助于某人。
Today I came across a rather weird phenomenon. When developing a Rails app in which every user has his own subdomain, and trying to use Devise to do it, I encountered that also subdomains not registered whatsoever would route to the root page. So, for example, even if there was no (explicit) subdomain, it would route me to the main application page. Maybe this has to do with my particular setup? I also tried with a new Rails project, but I got the same result. Anyone able to clarify this for me? Railscasts didn't do the trick.
Also, right now I'm using WEBrick, even though that's not what I'll be using in production, and I'm using the domain lvh.me to access the subdomains.
Thanks for the help.
EDIT: Here's my routes.rb file:
Rails.application.routes.draw do
devise_for :users
root 'static_page#index'
end
I followed this guide from the Devise wiki: https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain
So what I did was I removed the uniqueness of the email through a migration, and next I added :subdomain to request_keys as follows:
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :timeoutable, request_keys: [:subdomain]
Then I went on to override the "find_for_authentication" function, as per the following:
def self.find_for_authentication(conditions={})
conditions[:account_id] = Account.find_by_subdomain(conditions.delete(:subdomain)).id
super(conditions)
end
And that just about sums it up.
EDIT: I've been messing around, and I've found the problem. The 'root' directive refers all subdomains to my domain. So if I remove the root from my routes.rb, every subdomain leads to a RouteError. I remember about needing a root somewhere in my app for Devise. So I'm not sure if this is Devise's behaviour or root's.
I solved it manually. I found out the 'root' directive in the routes.rb accepts requests from every subdomain. In the end, I implemented a custom constraint and added this to the constraint for the devise_for in the routes.rb file. This adequately solved my problem. Thank you all for your help!
Update:
In response to the comment asking for my code: we had a system set up whereby each account (business account) would have their own subdomain, and each business account would have many users. My subdomain constraint looks like
lib/subdomain.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? && Account.exists?(subdomain: request.subdomain)
end
end
Then in my config/routes.rb I used that constrain to route the requests:
config/routes.rb
require 'Subdomain'
Rails.application.routes.draw do
# Some routes...
constraints(Subdomain) do
devise_for :users, controllers: {:sessions => 'session'}
root 'dashboard#index'
# More subdomain constrained routes...
end
end
In my original question I'd posted I'd already shown the rest of my setup. I hope this helps someone.
这篇关于Rails 4应用程序中的子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!