이것은 실제로 나를 괴롭혔던 이상한 오류입니다. 첫째, 배경.첫 번째 ID를 잃으면 form_for가있는 중첩 된 리소스
가 내 설정/routes.rb에 다음과 같은 중첩 된 자원을 가지고 :
scope :requirements => { :protocol => 'https' } do
resource :user
resources :orgs do
resources :members
resources :events
resources :levels
resources :attendances
end
resources :sessions, :only => [:new, :create, :destroy]
end
그런 다음 응용 프로그램/컨트롤러/levels_controller.rb에서 내가 가진 :
def edit
@org = Org.find(params[:org_id])
@level = OrgLevel.find(params[:id])
end
def update
@level = OrgLevel.find(params[:id])
if @level.update_attributes(params[:level])
flash[:success] = "Level details updated"
redirect_to @level
else
render 'edit'
end
end
마지막으로, 응용 프로그램에서/뷰/레벨/edit.html.erb, 내가 가진 :
<% provide(:title, "Edit #{@level.name} for #{@org.name}") %>
<div class="hero-unit">
<h2>Edit "<%= @level.name %>" membership level for <%= @org.name %></h2>
<div class="row">
<div class="span6 offset3">
<%= form_for [@org, @level], :url => org_level_path do |f| %>
<%= render 'shared/error_messages' %>
<table class="editor">
<tr>
<td class="label_x">
<%= f.label :name %>
</td>
<td colspan="3">
<%= f.text_field :name %>
</td>
</tr>
</table>
<% end %>
</div>
</div>
</div>
https://spot-macbook.local/orgs/55/levels/162/edit이 정상 호출하지만, 클릭의 결과 " 변경 사항 저장 "https://spot-macbook.local/orgs/162/levels/162에 리디렉션 결과 다음과 같은 오류 :
ActiveRecord::RecordNotFound in LevelsController#show
Couldn't find Org with id=162
Rails.root: /Users/ogod/Projects/rails_projects/nom_de_joye_app
Application Trace | Framework Trace | Full Trace
app/controllers/levels_controller.rb:71:in `correct_user'
Request
Parameters:
{"requirements"=>{"protocol"=>"https"},
"org_id"=>"162",
"id"=>"162"}
참고 ORG_ID가 변경되었음을"55 ","대신 "(162). 내가 도대체 뭘 잘못하고있는 겁니까?
감사합니다. 중첩 된 리소스 경로 구문 (부모, 자식)은 매번 나를 가져옵니다 ... –