Francis Ouellet
Francis Ouellet

Reputation: 505

Forms broken after namespace modification

I recently modified my Sessions.rb controller as it was fighting against the Devise Sessions controller for supremacy in my application. It seems to have worked out well. I modified my route.rb as mentioned in the answer.

Now I'm having a couple of issues in the default (without much changes since the scaffold) sessions forms.

config/routes.rb
  namespace :classroom do
    resources :registrations
    resources :sessions
end

classroom/sessions/index.html.erb
  <h1>Listing sessions</h1>

  <table>
    <tr>
      <th>Class size</th>
      <th>Course</th>
      <th>Description</th>
      <th>Location</th>
      <th>Name</th>
      <th>Price</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>

  <% @sessions.each do |session| %>
    <tr>
       <td><%= session.class_size %></td>
        <td><%= session.course_id %></td>
        <td><%= session.description %></td>
        <td><%= session.location_id %></td>
        <td><%= session.name %></td>
        <td><%= session.price %></td>
        <td><%= link_to 'Show', session %></td>
        <td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
        <td><%= link_to 'Destroy', session, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
  </table>
  <br />
  <%= link_to 'New Session', new_classroom_session_path %>  

The following links has stopped working and gives a "Could not find a valid mapping for" error message when loading /sessions/index.html.erb

<td><%= link_to 'Show', session %></td>
td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', session, method: :delete, data: { confirm: 'Are you sure?' } %></td>

Output of running rake routes:

classroom_sessions GET    /classroom/sessions(.:format)                     classroom/sessions#index
                        POST   /classroom/sessions(.:format)               classroom/sessions#create
  new_classroom_session GET    /classroom/sessions/new(.:format)           classroom/sessions#new
 edit_classroom_session GET    /classroom/sessions/:id/edit(.:format)      classroom/sessions#edit
      classroom_session GET    /classroom/sessions/:id(.:format)           classroom/sessions#show
                        PUT    /classroom/sessions/:id(.:format)           classroom/sessions#update
                        DELETE /classroom/sessions/:id(.:format)           classroom/sessions#destroy

I'm not too sure how to modify the 'Show' and 'Destroy' part of the link to make them work with the new namespace. Thanks in advance for any help. Much, much appreciated.

Thanks, Francis

Upvotes: 0

Views: 75

Answers (2)

Zippie
Zippie

Reputation: 6088

try with:

<td><%= link_to 'Show', classroom_session_path(session) %></td>
<td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', classroom_session_path(session), method: :delete, data: { confirm: 'Are you sure?' } %></td>

Look at here at a nice answer how to handle links with namespaced routes:

rails using link_to with namespaced routes

Upvotes: 1

Jo&#227;o Daniel
Jo&#227;o Daniel

Reputation: 8986

The name of the route is presented on the first column of the routes table. You should follow it on the link helper.

They should be:

<td><%= link_to 'Show', classroom_session_path(session) %></td>
<td><%= link_to 'Edit', edit_classroom_session_path(session) %></td>
<td><%= link_to 'Destroy', classroom_session_path(session), method: :delete, data: { confirm: 'Are you sure?' } %></td>

Upvotes: 1

Related Questions