2016-03-14 147 views
0

我有以下的routes.rb路由錯誤

resources :api_users, :as => :users 

get '/:controller(/:action(/:id))' 
post "/:controller(/:action(/:id))" 
get '/' => 'startsites#startsite' 

我稱之爲application_controller.rb了以下行動:

def change_locale 
    if Settings.language_supported?(params[:locale]) 
    session[:locale] = params[:locale] 
    I18n.locale = params[:locale] 
    end 


    case params[:goto] 
    when "user" 
     if current_user.nil? 
      redirect_to :action => :home 
     else 
     redirect_to :controller => :users 
     end 
    when "lecturer" 
     if current_user.nil? 
      redirect_to :action => :home 
     else 
      redirect_to :controller => :lecturers 
     end 
     else 
     redirect_to :startsites => :startsite 
    end 
    end 

,我得到這個錯誤:

No route matches [GET] "/settings/change_locale" 

我該如何解決這個問題?

回答

2

你只需要定義SettingsController

class SettingsController < ApplicationController 
    def change_locale 
    render text: 'it should be ok now' 
    end 
end 

它不能只是在ApplicationController的方法。

1

第一個路徑「/ settings」假定有設置控制器,第二個查找設置控制器動作,即change_locale。您正在定義applicationcontroller中的change_locale並將get請求發送給未定義的設置控制器。因此,錯誤發生No route matches [GET] "/settings/change_locale"

這可能是一個解決方案

class SettingsController < ApplicationController 
    def change_locale 
    if Settings.language_supported?(params[:locale]) 
    session[:locale] = params[:locale] 
    I18n.locale = params[:locale] 
    end 
end 

而且在路由文件

get '/settings/change_locale'