2013-03-19 101 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 
相关问题