浅嵌套路由仍在寻找父级的 id
对 Ruby on Rails 来说相对较新,但我认为我已经掌握了基础知识。我遇到了一个问题,但我无法弄清楚并且不知道如何调试。
我有浅层路线:
resources :incident_reports, :shallow => true do
get :thanks, :on => :collection
get :monthly_totals, :on => :collection
post :monthly_totals_download, :on => :collection
resources :supervisory_reviews, :comments
end
并且 rake 路线显示了我所期望的:
incident_report_supervisory_reviews GET /incident_reports/:incident_report_id/supervisory_reviews(.:format) {:action=>"index", :controller=>"supervisory_reviews"}
POST /incident_reports/:incident_report_id/supervisory_reviews(.:format) {:action=>"create", :controller=>"supervisory_reviews"}
new_incident_report_supervisory_review GET /incident_reports/:incident_report_id/supervisory_reviews/new(.:format) {:action=>"new", :controller=>"supervisory_reviews"}
edit_supervisory_review GET /supervisory_reviews/:id/edit(.:format) {:action=>"edit", :controller=>"supervisory_reviews"}
supervisory_review GET /supervisory_reviews/:id(.:format) {:action=>"show", :controller=>"supervisory_reviews"}
PUT /supervisory_reviews/:id(.:format) {:action=>"update", :controller=>"supervisory_reviews"}
DELETE /supervisory_reviews/:id(.:format) {:action=>"destroy", :controller=>"supervisory_reviews"}
incident_report_comments GET /incident_reports/:incident_report_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /incident_reports/:incident_report_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_incident_report_comment GET /incident_reports/:incident_report_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_comment GET /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
我已经显示和更新以及supervisory_reviews_controller中定义的其他所有内容,
def update
@supervisory_review = SupervisoryReview.find(params[:id])
respond_to do |format|
if @supervisory_review.update_attributes(params[:supervisory_review])
format.html { redirect_to(@supervisory_review.incident_report, :notice => 'Supervisory review was successfully updated.') }
format.xml { head :ok }
else
format.html { redirect_to(@supervisory_review.incident_report, :notice => 'An error occurred.') }
format.xml { render :xml => @supervisory_review.errors, :status => :unprocessable_entity }
end
end
end
但是当我尝试显示、更新或删除监督审查时 (表格示例)
<form accept-charset="UTF-8" action="/supervisory_reviews/1047" class="edit_supervisory_review" id="edit_supervisory_review_1047" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="ii8Xhygfcr71icNZfljlEkQItpV1+zFKaEn6ZZdYjiE=" />
(看起来不错,对吧?)
我得到“您正在寻找的页面不存在”
并且日志显示:
开始发布 “/supervisory_reviews/1047”对于 10.1.5.24 于 6 月 24 日星期五 17:14:21 -0 400 2011 处理者 SupervisoryReviewsController#更新为 HTML 参数: {“authenticity_token”=>“ii8Xhygfcr71icNZfljlEkQItpV1+zFKaEn6ZZdYji E=", "utf8"=>"âo"", "id"=>"1047", “supervisory_review”=>{“inproperty_reason”=
<块引用>“”,“advocacy_next_steps”=>“”,“决议”=>“”, “评论”=>“gfhsdfgsdfg”,“哈 ndling_of_incident"=>"", “建议”=>“dfsgdfggdfsd”, “需要宣传”= “假”,“事件已解决”=>“真”,“事件_处理适当”=>“真”} 2ms 内完成
ActiveRecord::RecordNotFound(无法 查找没有 ID 的事件报告):
lib/role_requirement_system.rb:121:incheck_roles'
check_roles'
lib/role_requirement_system.rb:121:in
那么为什么它要寻找 IncidentReport呢?
最奇怪的部分是注释工作正常,据我所知,这两个资源的代码是相同的。
Relatively new to Ruby on Rails but I think I have the basics down. I'm running into one problem though that i just can't figure out and don't really know how to debug.
I have shallow routes:
resources :incident_reports, :shallow => true do
get :thanks, :on => :collection
get :monthly_totals, :on => :collection
post :monthly_totals_download, :on => :collection
resources :supervisory_reviews, :comments
end
And rake routes shows what i expect:
incident_report_supervisory_reviews GET /incident_reports/:incident_report_id/supervisory_reviews(.:format) {:action=>"index", :controller=>"supervisory_reviews"}
POST /incident_reports/:incident_report_id/supervisory_reviews(.:format) {:action=>"create", :controller=>"supervisory_reviews"}
new_incident_report_supervisory_review GET /incident_reports/:incident_report_id/supervisory_reviews/new(.:format) {:action=>"new", :controller=>"supervisory_reviews"}
edit_supervisory_review GET /supervisory_reviews/:id/edit(.:format) {:action=>"edit", :controller=>"supervisory_reviews"}
supervisory_review GET /supervisory_reviews/:id(.:format) {:action=>"show", :controller=>"supervisory_reviews"}
PUT /supervisory_reviews/:id(.:format) {:action=>"update", :controller=>"supervisory_reviews"}
DELETE /supervisory_reviews/:id(.:format) {:action=>"destroy", :controller=>"supervisory_reviews"}
incident_report_comments GET /incident_reports/:incident_report_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /incident_reports/:incident_report_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_incident_report_comment GET /incident_reports/:incident_report_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_comment GET /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
i have show and update and everything else defined in supervisory_reviews_controller
def update
@supervisory_review = SupervisoryReview.find(params[:id])
respond_to do |format|
if @supervisory_review.update_attributes(params[:supervisory_review])
format.html { redirect_to(@supervisory_review.incident_report, :notice => 'Supervisory review was successfully updated.') }
format.xml { head :ok }
else
format.html { redirect_to(@supervisory_review.incident_report, :notice => 'An error occurred.') }
format.xml { render :xml => @supervisory_review.errors, :status => :unprocessable_entity }
end
end
end
but when i try to show, update or delete a supervisory review
(form example)
<form accept-charset="UTF-8" action="/supervisory_reviews/1047" class="edit_supervisory_review" id="edit_supervisory_review_1047" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="ii8Xhygfcr71icNZfljlEkQItpV1+zFKaEn6ZZdYjiE=" />
(looks right, right?)
i get "The page you were looking for doesn't exist"
and the log says:
Started POST
"/supervisory_reviews/1047" for
10.1.5.24 at Fri Jun 24 17:14:21 -0 400 2011 Processing by
SupervisoryReviewsController#update as
HTML Parameters:
{"authenticity_token"=>"ii8Xhygfcr71icNZfljlEkQItpV1+zFKaEn6ZZdYji
E=", "utf8"=>"âo"", "id"=>"1047",
"supervisory_review"=>{"inappropriate_reason"="", "advocacy_next_steps"=>"", "resolution"=>"",
"comments"=>"gfhsdfgsdfg", "ha
ndling_of_incident"=>"",
"recommendations"=>"dfsgdfggdfsd",
"need_for_advocacy"=
"false", "incident_resolved"=>"true", "incident_handled_appropriately"=>"true"}
} Completed in 2msActiveRecord::RecordNotFound (Couldn't
find IncidentReport without an ID):
lib/role_requirement_system.rb:121:incheck_roles'
check_roles'
lib/role_requirement_system.rb:121:in
so why is it looking for an IncidentReport?
the weirdest part is Comments work fine and as far as i can tell, the code for both resources is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不在于路由,而在于 lib/role_requirement_system.rb 第 121 行。也许该代码需要 IncidentReport 来提供授权。如果您需要更多帮助,请发布该库中的代码。
The issue is not with the routes but with the lib/role_requirement_system.rb line 121. Perhaps that code needs an IncidentReport to provide authorization. Post the code from that library if you need more help.