我已经阅读了一些类似的问题/答案,但找不到我的错误的任何答案。
我有一个用于输入工作小时数的表格和一个用于编辑小时记录的表格。根据 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