0

这是一个奇怪的错误,真的让我很烦。首先,一些背景。

我的 config/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

然后,在 app/controllers/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

最后,在 app/views/levels/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”。我究竟做错了什么?

4

1 回答 1

1

嗬!

发布此问题五秒钟后,我意识到存在错误并进行了更正。

原来的更新方法如下:

    redirect_to @level

这应该是:

    redirect_to org_level_path(@org, @level)

这么简单的错误,但我找错地方了!

于 2013-03-04T14:11:49.643 回答