Rails 4 动态子域 [英] Rails 4 Dynamic Subdomains
问题描述
我正在学习 http://railscasts.com/episodes 上的教程/221-subdomains-in-rails-3 并试图使其适用于 rails 4.我遇到的问题是在我的控制器和 find_by_subdomain 中!标记,我已经阅读了大部分从 rails 4 中取出的 find_by 方法,并且想知道解决方法是什么.
Hi I am following the tutorial at http://railscasts.com/episodes/221-subdomains-in-rails-3 and trying to make it work for rails 4. The problem I encountered is in the my controller and with the find_by_subdomain! tag, I have read that most of the the find_by methods where taken out of rails 4, and was wondering what the work around was.
我的控制器目前看起来像
my controller currently looks like
def set_city
@city = City.friendly.find_by_subdomain!(request.subdomain)
end
我得到的错误是
undefined method `find_by_subdomain!'
此外,如果它有助于我目前的路线看起来像
Also in case it helps my routes currently look like
get '/' => 'cities#show', :constraints => { :subdomain => /.+/ }
非常感谢任何帮助,如果需要,我很乐意澄清.
Any help would by greatly appreciated and I would be happy to clarify if needed.
推荐答案
find_by_*
方法在类本身上:
City.find_by_subdomain!(request.subdomain)
City.friendly
返回了什么?不管它是什么,我怀疑它是 City
类.
What is City.friendly
returning? Whatever it is, I doubt it's the City
class.
此外,您现在可以将 find_by
与哈希一起使用:
Also, you can use find_by
with a hash now:
City.find_by subdomain: request.subdomain
http://guides.rubyonrails.org/active_record_querying.html#retrieving-a-single-对象
这篇关于Rails 4 动态子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!