2012-11-29 36 views
1

我認爲我絆倒渲染/重定向問題。在我的Rails應用程序中,我有可以有許多項目的客戶。當我更新一個項目時,我使用了form_for方法的嵌套表單,分別向它傳遞一個客戶端和項目實例變量。Rails - 第一行表單中未定義的方法`project_path'?

這裏是我的代碼:

的routes.rb:

resources :clients do 
    resources :projects 
end 

projects_controller.rb

def edit 
    @client = Client.find(params[:client_id]) 
    @project = Project.find(params[:id]) 
end 

def update 
    @project = Project.find(params[:id]) 

    respond_to do |format| 
    if @project.update_attributes(params[:project]) 
     format.html { redirect_to client_project_path(@project.client, @project), notice: 'Project was successfully updated.' } 
     format.json { head :no_content } 
    else 
     format.html { render action: "edit" } # !!this seems to be the problem 
     format.json { render json: @project.errors, status: :unprocessable_entity } 
    end 
    end 
end 

的意見/項目/ _form.html.erb:

<%= simple_form_for([@client, @project]) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :title %> 
    <%= f.input :start_on %> 
    <%= f.input :end_on %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

當我只是顯示編輯活動ñ,一切正常。如果我用有效的數據更新項目,一切都很好。當我在表單中輸入無效或缺少的數據時會出現問題 - 然後調用更新方法的「其他部分」,並且應該呈現「編輯」操作。

這確實發生了,但在我看來,什麼是錯或者與「編輯」方式或自己的狀態,因爲當我改變render action: "edit"redirect_to action: "edit",它重定向我(當然沒有錯誤消息以便清晰地不是所需的解決方案,但我只是想測試)。

當「編輯」被渲染,這是錯誤消息我得到:

undefined method `project_path' 

這僅僅是指在我的表格的第一行。

我在這裏做錯了什麼?對不起,長期的問題,謝謝你的幫助!

回答

3

您需要在更新操作中加載@client變量。這樣想一下,如果你點擊了else,編輯提供給視圖的是什麼,因爲當你渲染而不是重定向時,你只需要這個。由於您的資源是嵌套的,form_for助手需要客戶端和項目。

+0

當然,就是這樣!正是我需要的。謝謝。 – weltschmerz

相關問題