2010-08-09 154 views
0

我還是Ruby on Rails的新手,我目前遇到了路由和重定向的問題。重定向和路由Ruby on Rails

我有兩個控制器「候選人」和「調查」。一旦點擊提交按鈕,我現在正在收看「canidate/editEmail.html」

http://www.otoplusvn.com/TherapistSurvey/candidates/editEmail/2

,執行考生的更新的電子郵件方法:

def updateEmail 
    @candidate = Candidate.find(params[:id]) 

    respond_to do |format| 
     if @candidate.update_attributes(params[:candidate]) 
     flash[:notice] = 'Candidate email was successfully updated.' 
     format.html { redirect_to :controller => "survey", :action => "end" } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @candidate.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

此代碼將更新候選人的電子郵件在數據庫中並重定向。我想將其重定向到位於view/Survey/end.html.erb下的名爲end.html.erb的html頁面。但是,使用目前的代碼,它會給我一個

Unknown action 

No action responded to show. Actions: download and lastPage 

其中download和lastPage是調查控制器中唯一的方法。 「end」不是在survey_controller.rb中定義的方法,但我認爲它會自動跳轉到位於views/survey下的end.html.erb在我的本地機器上,我能夠得到這個這個更新的代碼工作:

format.html { redirect_to :controller => "survey/end" } 

但這不是我裝上軌應用的紅寶石在現場工作。在任何一種情況下,我認爲可能有一種更簡單的方法來做到這一點,我做錯了。

我認爲它必須處理route.rb文件或許可證,當您將導航應用程序中的本地ruby移植到主機時,我並不知道這些文件或權限。

通常我會想,如果我直接去了文件

http://www.otoplusvn.com/TherapistSurvey/survey/end

我能看到的網頁就像我能在我的本地機器上,但我不能用我託管的東西。

一般而言,我只是希望能夠重定向(跳轉)到另一個HTML頁面,表明電子郵件更新已正確完成,並且調查已結束。這一切都是通過點擊一個提交按鈕觸發的,這個按鈕導致數據庫更新被稱爲重定向到另一個頁面。問題是如何正確執行重定向。

BTW:在我的routes.rb我有

map.resources :survey 

知道的任何建議。謝謝! d

回答

1

你需要添加你想要的所有其他行動。

地圖。資源僅通過資源瀏覽添加路由。

所以加在收集選項的方法結束你的情況

map.resources :survey, :collection => {:end => :get} 
+0

我加了它,但我仍然得到未知的動作。 我做了一個耙路徑來顯示路徑。 PUT /candidates/:id(.:format){:controller =>「candidates」,:action =>「update」} DELETE /candidates/:id(.:format){:controller =>「candidates」 ,:action =>「destroy」} end_survey GET /survey/end(.:format){:controller =>「survey」,:action =>「end」} – darewreck 2010-08-09 10:03:43

+0

「end」實際上必須定義爲行動還是把它作爲一個end.html.erb足夠的查看/調查? – darewreck 2010-08-09 10:08:46

+0

我也更新了「end」到「lastPage」,所以它不會與關鍵詞混淆,最終點擊otoplusvn.com/TherapistSurvey/candidates/editEmail/2,你會得到一個頁面otoplusvn.com/TherapistSurvey/survey/lastPage - – darewreck 2010-08-10 07:22:05

1

添加這routes.rb中的應用程序的路徑,從終端

map.resources :survey, :collection => {:end => :get} 

運行rake路線,看看你的路由。

使用已命名的路線:

redirect_to survey_end_path 
+0

如何定義survey_end_path。我更新了代碼以反映這一點,但它給了我 未定義的局部變量或方法'survey_end_path'爲# darewreck 2010-08-09 10:00:33

+0

我也嘗試過「end_survey_path」,因爲這是當您執行「rake路由時打印出來的內容。 「它打印出「end_survey」。但仍然沒有去 – darewreck 2010-08-09 10:13:16

+0

我也更新了「end」到「lastPage」,所以它不會與關鍵詞結尾相混淆 click http://www.otoplusvn.com/TherapistSurvey/candidates/editEmail/2 and you'我會得到一個頁面http://www.otoplusvn.com/TherapistSurvey/survey/last – darewreck 2010-08-10 07:21:43