我已经阅读了一些类似的问题/答案,但找不到我的错误的任何答案。
我有一个用于输入工作时间的表格和一个用于编辑工时记录的表格。根据Rails指南,添加和编辑共享部分。我的添加(新)表单工作正常。提交编辑表单时,我收到错误/错误:
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| %>
这应该转到'hours'控制器,转到'update'动作。也许我的控制器还有其他问题,但添加/新表单工作得很好......令人困惑......
我的控制器:
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
答案 0 :(得分:1)
试试这个
<% form_for @hours do |f| %>
或者
<% form_for :hours, :url =>
url_for(:action => "update", :id => @hours) do |f| %>
答案 1 :(得分: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