2013-08-05 17 views
2

我是Ruby on Rails中的新成員,這是我在stackoverflow上的第一篇文章。redirect_to導致與請求不同的動作

我有這個頁面,用戶可以選擇他想要的功能。由於他選擇了這些功能,因此該操作會將他帶到一個頁面,在該頁面中可以看到所選功能的屬性,並且對於每個選定的功能都有一個用於編輯該功能的鏈接。 我可以完美地編輯和更新功能,但我希望動作更新可以重定向到具有選定功能的頁面,以便用戶不必再次選擇它們。

問題是,當我做redirect_to select_multiple_features_path時,它不會導致select_multiple動作。我收到以下錯誤:


的ActiveRecord :: RecordNotFound在FeaturesController#顯示

未能找到ID功能= select_multiple

服務器輸出:http://img138.imageshack.us/img138/1382/sfp4.jpg


控制器:

def edit 
    @feature = Feature.find(params[:id]) 
    end 

    def update 
    @feature = Feature.find(params[:id])  
    respond_to do |format| 
     if @feature.update_attributes(params[:feature]) 
     format.html { redirect_to select_multiple_features_path, notice: 'Feature was successfully updated.' } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @feature.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def select_multiple 
    if params[:features_ids].nil? 
     @features = Feature.find(session[:features_ids]) 
    else 
     @features = Feature.find(params[:features_ids]) 
     session[:features_ids] = params[:features_ids] 
    end 
    end 

路線:

resources :attached_assets 

resources :modifications 

resources :maps 

resources :coordinates 

resources :results do 
    collection do 
     post 'select_number_of_groups' 
    end 
end 


resources :features do 
    collection do 
     post 'select_multiple' 
    end 
end 

resources :home 

devise_for :users, :path => "auth", :path_names => { :sign_in => 'login', 
                :sign_out => 'logout', 
                :password => 'secret', 
                :confirmation => 'verification', 
                :unlock => 'unblock', 
                :registration => 'register', 
                :sign_up => 'cmon_let_me_in' 
                } 

devise_scope :user do 
    root to: "devise/sessions#new" 
end 

任何想法?

+1

請告訴我們你的'配置/ routes.rb'文件。 –

+0

我編輯帖子把routes.rb –

+0

請更改此 後'select_multiple'以在您的routes.rb文件中獲得'select_multiple',並檢查。 –

回答

0

在您的路線中,您正在定義您的post操作,但您並未定義您的get操作。 你必須定義:

get 'features/select_multiple' => 'features#select_multiple' 

否則它會假設你想在默認情況下使用#show行動。

您可以使用rake routes在你的終端能顯示你所有的路線和配套措施

+0

謝謝。這解決了問題:) –

相關問題