0
我都或多或少執行了這項railscast獲得子域:重定向不匹配的子域名根
http://railscasts.com/episodes/221-subdomains-in-rails-3
它的偉大工程,但我想不匹配的子域名重定向到根路徑。
這是routes.rb中的一部分:
match '/' => 'menus#show', constraints: { subdomain: /^(?!www$)(.+)$/i }
而且這是在菜單#顯示:
# GET /menus/1
# GET /menus/1.json
def show
if !params[:id].nil?
@menu = Menu.find(params[:id])
else
if Shop.find_by_subdomain(request.subdomain).nil?
respond_to do |format|
format.html { redirect_to(root_path, :notice => I18n.t("misc.not_found")) }
return
end
else
@menu = Shop.find_by_subdomain(request.subdomain).menus.last
end
end
if [email protected]?
respond_to do |format|
format.html # show.html.erb
format.json { render json: @menu }
end
end
end
所以我還是想保持不使用子域名的可能性(在爲此檢查params [:id]。然後我檢查子域是否存在,如果不是我想重定向。
目前我在嘗試一個不存在的域名時遇到了這個問題,所以我相信他仍然試圖如何菜單:
undefined method `shop' for nil:NilClass
Extracted source (around line #1):
1: <h2>Menu <%= @menu.shop.name %></h2>
2: <br>
3: <%= render :partial => 'menu_show' %>
嗨Javid,謝謝你的回答。這導致了一個循環,我編輯你的答案,所以它的工作原理。謝謝! – rept