0

This is a strange error that has really got me bugged. First, some background.

I have the following nested resources in my 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

Then, in app/controllers/levels_controller.rb I have:

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

Finally, in app/views/levels/edit.html.erb, I have:

<% 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>

The result of calling https://spot-macbook.local/orgs/55/levels/162/edit is normal, but clicking "Save Changes" results in a redirection to https://spot-macbook.local/orgs/162/levels/162 and the following error:

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"}

Note that the org_id has changed to "162" instead of "55". What am I doing wrong?

1 Answer 1

1

Doh!

Five seconds after I post this question, I realised there error and corrected it.

The original has the update method with the following:

    redirect_to @level

This should be:

    redirect_to org_level_path(@org, @level)

Such a simple error, but I was looking in the wrong place!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The nested resources path syntax with (parent, child) gets me every time...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.