问题
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