2013-03-04 81 views
0

这是一个奇怪的错误,真的让我迷惑。首先,一些背景。嵌套资源form_for丢失第一个id

,我有以下嵌套的资源我的配置/ 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 &quot;<%= @level.name %>&quot; 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已更改为‘162’,而不是‘55’。我究竟做错了什么?

回答

1

Doh!

我发布这个问题后五秒钟,我意识到有错误,并纠正它。

原来一直用以下更新方法:

redirect_to @level 

这应该是:

redirect_to org_level_path(@org, @level) 

这样一个简单的错误,但我一直在寻找在错误的地方!

+0

谢谢! (父,子)的嵌套资源路径语法让我每次都... – 2013-04-29 00:28:19