Rails 4 Dynamic Subdomains

点点圈 提交于 2019-12-07 22:05:43

问题


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

and the error I am getting is

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.


回答1:


The find_by_* method is on the class itself:

City.find_by_subdomain!(request.subdomain)

What is City.friendly returning? Whatever it is, I doubt it's the City class.

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-object



来源:https://stackoverflow.com/questions/19885271/rails-4-dynamic-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!