0

I'm trying to create an edit page for a nested route.

The url is something like: http://localhost:3000/clients/2/notes/3/edit

my routes.rb:

 resources :clients do
    resources :notes
  end

In my edit controller:

def edit
  @note = Note.find(params[:id])
  @client = Client.find(params[:client_id])
end

and my edit.html.erb file

<%= form_for(@client, @note) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= f.label :content %>
    <%= f.text_field :content %> 
    <%= f.submit "Save changes" %>
<% end %>

When I do this and load the edit page, I get the following: enter image description here

I've looked around on stack overflow and they all have two arguments when using nested routes, what is the correct thing to do here? And why is it different?

Update:

rake routes
           users GET    /users(.:format)                             users#index
                 POST   /users(.:format)                             users#create
        new_user GET    /users/new(.:format)                         users#new
       edit_user GET    /users/:id/edit(.:format)                    users#edit
            user GET    /users/:id(.:format)                         users#show
                 PUT    /users/:id(.:format)                         users#update
                 DELETE /users/:id(.:format)                         users#destroy
        sessions POST   /sessions(.:format)                          sessions#create
     new_session GET    /sessions/new(.:format)                      sessions#new
         session DELETE /sessions/:id(.:format)                      sessions#destroy
    client_notes GET    /clients/:client_id/notes(.:format)          notes#index
                 POST   /clients/:client_id/notes(.:format)          notes#create
 new_client_note GET    /clients/:client_id/notes/new(.:format)      notes#new
edit_client_note GET    /clients/:client_id/notes/:id/edit(.:format) notes#edit
     client_note GET    /clients/:client_id/notes/:id(.:format)      notes#show
                 PUT    /clients/:client_id/notes/:id(.:format)      notes#update
                 DELETE /clients/:client_id/notes/:id(.:format)      notes#destroy
         clients GET    /clients(.:format)                           clients#index
                 POST   /clients(.:format)                           clients#create
      new_client GET    /clients/new(.:format)                       clients#new
     edit_client GET    /clients/:id/edit(.:format)                  clients#edit
          client GET    /clients/:id(.:format)                       clients#show
                 PUT    /clients/:id(.:format)                       clients#update
                 DELETE /clients/:id(.:format)                       clients#destroy
            root        /                                            clients#index
          signup        /signup(.:format)                            users#new
          signin        /signin(.:format)                            sessions#new
         signout DELETE /signout(.:format)                           sessions#destroy
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Noah Clark
  • 8,101
  • 14
  • 74
  • 116
  • whats the result of `rake routes`? – Çağdaş Jun 11 '12 at 14:40
  • 2
    Try using brackets.. <%= form_for([@client, @note]) do |f| %> – MurifoX Jun 11 '12 at 14:41
  • @Çağdaş I've added rake routes. – Noah Clark Jun 11 '12 at 14:48
  • @MurifoX adding brackets gets me: `undefined method `content' for nil:NilClass` which I think is actually an improvement. It means it's not finding notes, I think. – Noah Clark Jun 11 '12 at 14:49
  • @Rob, I haven't defined to_param, I don't think. Is that something I would do in the controller? – Noah Clark Jun 11 '12 at 14:51
  • @MurifoX Sorry, that fixed it. I was editing a bit of other code and got that other error. If you post it as an answer, I'll accept it as the correct one. Do the brackets pass it as a single unit? – Noah Clark Jun 11 '12 at 14:54
  • Just posted it as an answer. I have struggled with this issue in the past. The brackets means it is an array. Rails interprets this values and builds a nested form with it. – MurifoX Jun 11 '12 at 16:41

1 Answers1

1

Try using brackets..

<%= form_for([@client, @note]) do |f| %>
MurifoX
  • 14,991
  • 3
  • 36
  • 60