2012-01-18 38 views
1

我遇到了類似的問題,因爲此前SO question,但答案似乎不適合我。所以我想跟進我的問題。Rails 3控制器更新操作在本地工作,但不在heroku上,失去模板錯誤

我在我的控制器中使用複選框更新我的配置文件模型。當我點擊與本地運行的應用程序的複選框,我沒有任何問題。然而,當我部署到Heroku上我得到:

ActionView::MissingTemplate (Missing template profiles/show_hometown_settings with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/app/app/views", "/app/vendor/plugins/rails_log_stdout/app/views", "/app/vendor/plugins/rails3_serve_static_assets/app/views", "/app/vendor/plugins/paperclip/app/views"): 

我添加既是_show_hometown_settings.html.erb文件和_show_hometown_settings.js.erb文件,但也沒有獲得成功。檢查我的複選框給我在我的屏幕上的香草代碼和上述錯誤。

如果我添加show_hometown_settings.html.erb我不再獲得香草代碼。我看到show_hometown_settings.html.erb模板,但仍然出現錯誤,我沒有被重定向到我的settings_path,這是整點(檢查更新,發佈到數據庫,重定向進行進一步的設置更改)。任何人都可以幫我解決這個問題嗎?

這裏是控制器動作:

def show_hometown_settings 
    @profile = current_user.profile 
    if @profile.show_hometown == true 
    if @profile.update_attributes(:show_hometown => false) 
     redirect_to settings_path 
    else 
     redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.' 
    end 
    elsif @profile.show_hometown == false 
    if @profile.update_attributes(:show_hometown => true) 
     redirect_to settings_path 
    else 
    redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.' 
    end 
    end 
end 

這是我使用的動作形式:

<%= form_tag({:action => "show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do %> 
    <%= check_box_tag(:show_hometown, 1, @user.profile.show_hometown ? true : false) %> 
    <%= @user.profile.hometown %> 
<% end %> 

UPDATE:這裏是我的routes.rb文件的部分引用的操作:

match "profiles/show_hometown_settings", :to => "profiles#show_hometown_settings" 

更新2:低於繼的問題,我有一個不同的錯誤,我的日誌顯示的路線問題:

這將返回

`The page you were looking for doesn't exist (404)` 

在我heroku logs --tail我看到

2012-01-19T23:13:47+00:00 app[web.1]: Started POST "/profiles/show_hometown_settings" for 98.218.231.113 at 2012-01-19 23:13:47 +0000 
2012-01-19T23:13:47+00:00 app[web.1]: 
2012-01-19T23:13:47+00:00 app[web.1]: ActionController::RoutingError (No route matches "/profiles/show_hometown_settings"): 

如果我將路線從:via => [:put]更改爲:via => [:post]它的工作原理。希望沒關係。

+0

你確定你有相關的文件在git中提交? – zsquare 2012-01-18 13:53:34

+0

幾乎肯定。如果我提交'show_hometown_settings',我的想法會變得很糟糕,但我仍然遇到錯誤。 – tvalent2 2012-01-18 15:17:44

+0

您是否嘗試過在重定向後添加'return'?它聽起來像Rails試圖渲染show_hometown_settings動作,即使每個邏輯路徑都會導致重定向。 – MrTheWalrus 2012-01-18 16:46:34

回答

1

因爲您沒有渲染視圖,所以您應該不需要擁有該模板文件,而只是重定向。

什麼是可能發生的是,你的if-elsif邏輯被打破,代碼永遠不會達到那些重定向(因爲無論是if塊都計算到true)。如果Rails沒有得到redirect_to調用,那麼默認情況下它將呈現控制器操作的視圖(這是它正在嘗試執行的操作)。

我會改變周圍的話對這:

路線:

match "profiles/show_hometown_settings", :to => "profiles#show_hometown_settings", :as => :show_hometown_settings, :via => [:put] 

控制器:

def show_hometown_settings 
    if current_user.profile.update_attributes(:show_hometown => params[:show_hometown]) 
    redirect_to settings_path 
    else 
    redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.' 
    end 
end 

形式:

<%= form_tag(show_hometown_settings_path, :method => :put) do %> 
    <%= check_box_tag(:show_hometown, 1, @user.profile.show_hometown ? true : false) %> 
    <%= @user.profile.hometown %> 
<% end %> 
+0

我已經更新了我的問題,並得到了我所得到的結果。我覺得我越來越近了...... – tvalent2 2012-01-19 23:15:52

+0

如果我把''via'從'put'改成'post',它就行得通!我將不得不回顧一下我的「if-else」邏輯,看看我沒有做好什麼。謝謝! – tvalent2 2012-01-19 23:22:56

+1

@ tvalent2'POST'用於在數據庫中創建記錄。既然你正在更新它們,你應該使用'PUT'。請將':method =>:put'添加到'form_tag'中,如上面我更新的答案中所示,它應該爲您解決該問題。別客氣! :) – iwasrobbed 2012-01-19 23:35:26

相關問題