BluePython
BluePython

Reputation: 1597

Calling for list but getting error "The action 'show' could not be found for SubjectsController"

I am following the Lynda tutorials on rails and I have defined the list action in addition to the list.html.erb file with corresponding information.

However when I proceed to access the ~/subjects/list I get this:

Unknown action

The action 'show' could not be found for SubjectsController

This is the code from my controller:

class SubjectsController < ApplicationController

  def list

    @subjects = Subject.order("subjects.position ASC")

  end

end

Thank you!

Update: This is what I am getting from Rake routes

 rake routes
    subjects GET    /subjects(.:format)          subjects#index
             POST   /subjects(.:format)          subjects#create
 new_subject GET    /subjects/new(.:format)      subjects#new
edit_subject GET    /subjects/:id/edit(.:format) subjects#edit
     subject GET    /subjects/:id(.:format)      subjects#show
             PUT    /subjects/:id(.:format)      subjects#update
             DELETE /subjects/:id(.:format)      subjects#destroy

UPDATE:

Code for the view list.html.erb:

    <div class="subject list">
  <h2>Subjects</h2>

  <table class="listing" summary="Subject list">
    <tr class="header">
      <th>&nbsp;</th>
      <th>Subject</th>
      <th>Visible</th>
      <th>Pages</th>
      <th>Actions</th>
    </tr>
    <% @subjects.each do |subject| %>
    <tr>
      <td><%= subject.position %></td>
      <td><%= subject.name %></td>
      <td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
      <td class="center"><%= subject.pages.size %></td>
      <td class="actions">
        <%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
        <%= link_to("Edit", '#', :class => 'action edit') %>
        <%= link_to("Delete", '#', :class => 'action delete') %>
      </td>
    </tr>
    <% end %>
  </table>
</div>

Upvotes: 1

Views: 465

Answers (3)

maxhm10
maxhm10

Reputation: 1042

Just recently went past the exact same problem. As SDP pointed out, there are some differences between Rails 3.x and Rails 4. Rails 4 needs the Show template to be already available, even if you haven't clicked the Show button on your subjects/list url. The easiest way to solve your problem is to temporarily put a placeholder ('#') in the links in your view until the next chapter of the tutorial, where you get to code the Show template and action.

Upvotes: 0

emersonthis
emersonthis

Reputation: 33398

I have not found a changelog confirming this, but I highly suspect that the problem you are having is due to a difference between Rails 3.x and Rails 4.

I recently went through the same Lynda.com tutorial and he is definitely using Rails 3 and there are a handful of places where things he does do not line up perfectly with how Rails 4 works. For example, when he generates migrations there are up and down methods whereas in Rails 4 I get a single change method.

So I'm pretty sure your issue is another example of the this because I, like you, see that resources :controllername does not result in a route for controllername/list. My hunch is that /list has been merged into the /index action (which is how I sidestepped the issue), but I'm not enough of an authority to say whether or not this is a best practice.

Upvotes: 0

sauronnikko
sauronnikko

Reputation: 5455

You have to modify your resources :subjects line to make it look like this

resources :subjects do
  get 'list', on: :collection
end

This way, a subjects/list url will become available in your app.

Let me explain myself more: resources :subjects creates by default 7 routes (urls), which are related to 7 actions in your SubjectsController (for example, the GET http resquest type + /subjects url will call the index action). Look at your rake routes output (from your question post) to understand more. If you want to have more urls which begin with subjects/, you can add lines like the following inside the resources block:

get 'list', on: :collection

That just means a subjects/list url called by the GET http method will be created. The on: collection means the url won't be something like subjects/1/list.

For more information, I recommend reading Rails Routing from the Outside In

Upvotes: 2

Related Questions