1

我已经阅读了一些类似的问题/答案,但找不到我的错误的任何答案。

我有一个用于输入工作小时数的表格和一个用于编辑小时记录的表格。根据 Rails 指南,添加和编辑都共享部分内容。我的添加(新)表单工作正常。提交编辑表单时,我收到错误/错误:

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

我在这个屏幕上没有其他输出。

这些屏幕显示的 url 是:

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

小时部分的我的路线文件

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

任何关于如何解决这个问题的想法都将不胜感激。--jc

4

2 回答 2

1

像这样试试

 <% form_for @hours do |f| %>

要不然

<% form_for :hours, :url => 
     url_for(:action => "update", :id => @hours) do |f| %>
于 2013-03-19T11:09:49.527 回答
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
于 2016-03-02T22:03:08.007 回答