2013-03-19 58 views
1

我已閱讀了幾個類似的問題/答案,但找不到我的錯誤的任何答案。未知行動 - 無法找到控制器的行動

我有一個表單用於輸入工作小時數,另一個用於編輯小時記錄。按照Rails指導原則,添加和編輯共享一個部分。我的添加(新)表單工作正常。當提交編輯表單時,我得到錯誤/ bug:

Unknown Action 
The action '11' could not be found for HoursController 

我在此屏幕上沒有其他輸出。

這些畫面中顯示的網址是:

http://localhost:3000/hours/11/edit 

後提交:

http://localhost:3000/hours/11 

我的編輯表單的表單標籤是:

<%= form_for(:hour, :url => {:action => 'update', :id => @hour.id}) do |f| %> 

這應該去'小時'控制器,以'更新'行動。也許還有別的東西錯了我的控制,但添加/新形式的正常工作......彆扭......

我的控制器:

class HoursController < ApplicationController 

    before_filter :confirm_logged_in 

    def index 
    end 

    def list 
    if !params[:job_id].nil? 
     @hours = Hour.where(["job_id = ?",params[:job_id]]).date_sorted 
    else 
     @hours = Hour.date_sorted 
    end 
    end 

    def show 
    @hour = Hour.find(params[:id]) 
    end 

    def new 
    @hour = Hour.new 
    end 

    def create 
    @hour = Hour.new(params[:hours]) 
    if @hour.save 
     job = Job.find(params[:hours][:job_id]) 
     flash[:notice] = "You have just entered hours for #{job.name}" 
     redirect_to(:action => "list", :job_id => params[:hours][:job_id]) 
    else 
     flash[:notice] = "There were one or more problems with your form. Please try again!" 
     render('new') 
    end 
    end 

    def edit 
    @hour = Hour.find(params[:id]) 
    end 

    def update 
    @hour=Hour.find(params[:id]) 
    if @hour.update_attributes(params[:hour]) 
     flash[:notice] = "You have just updated an Hours entry" 
     redirect_to(:action => "list", :job_id => params[:hours][:job_id]) 
    else 
     render("edit") 
    end 
    end 

    def delete 
    @hour = Hour.find(id) 
    end 

    def destroy 
    Hour.find(params[:id]).destroy 
    flash[:notice] = "Hours Deleted Successfully!" 
    redirect_to(:action => 'list') 
    end 

    def search 
    if params[:job][:id] 
     @job = Job.find(params[:job][:id]) 
     redirect_to(:action => "list", :job_id => @job.id) 
    end 
    end 

end 

我routes文件的時間段

resources :hours do 
    collection do 
     get :list 
     get :delete 
    end 
end 

這給了我:

list_hours GET /hours/list(.:format)     hours#list 
    delete_hours GET /hours/delete(.:format)    hours#delete 
      hours GET /hours(.:format)      hours#index 
        POST /hours(.:format)      hours#create 
     new_hour GET /hours/new(.:format)     hours#new 
     edit_hour GET /hours/:id/edit(.:format)    hours#edit 
      hour GET /hours/:id(.:format)     hours#show 
        PUT /hours/:id(.:format)     hours#update 
        DELETE /hours/:id(.:format)     hours#destroy 

如何算出這個任何想法將大大apprec iated。 --jC

回答

1

嘗試這樣

<% form_for @hours do |f| %> 

否則

<% form_for :hours, :url => 
    url_for(:action => "update", :id => @hours) do |f| %> 
+0

<%的form_for @hours做| F | %> – 2013-03-19 11:59:55

+0

想說,謝謝。這工作。我一直在關注在線教程,所以我不知道這是問題所在。非常感謝。 <%form_for @hours do | f | %>工作! – 2013-03-19 12:01:24

+0

@JohnCowan通過接受我的回答在我的回答的左側說出您的感謝 – 2013-03-19 12:02:45

0

我的情況略有不同,因爲我有一個自定義的路線,但它可以幫助別人(或未來的我) 。

我不斷收到該錯誤,直到我將method: :post添加到窗體參數。

路線:

resources :hours do 
    member do 
    post :fix 
    end 
end 

html.erb

form_for @hour, url: fix_hour_path(@hour), method: :post do 
    ... 
end