turbo2oh
turbo2oh

Reputation: 2879

rails 3 : having trouble with routing

I'm getting the following error: No route matches {:action=>"show", :controller=>"events"}

My routes.rb file has the following entry:

resources :events

My events_controller.rb has the following:

 def index
@events = Event.all     
end

def show
@event = Event.find(params[:id])    
end

my show events page has the following:

<h1>SHOW EVENT DETAILS</h1>
<%= @currevent.name %>
<br/><br/>
<%= button_to "Back",event_path %>

Any ideas why I'm getting this error? Thanks in advance!

Forgot to include my index.html.erb for events:

<table border="1">
<% @events.each do |event| %>
<tr>    
    <td><%= event.name %></td>
     <td><%= button_to 'Show', event %></td>

</tr>
<% end %>
</table>

<br/><br/>
<%= button_to "Back",home_path %>

Upvotes: 0

Views: 117

Answers (5)

Steve Wilhelm
Steve Wilhelm

Reputation: 6260

There are two approaches. The simplest would be go to a new temporary directory and create a new clean Rails 3 project running the following commands:

cd /tmp 
rails new tst 
cd tst 
rails generate scaffold event

You can then look at the apps/config/routes.rb, apps/controller/events_controller.rb, and the apps/views/events files to see the how scaffold created file differ from your current code.

Alternatively, you could try to debug your existing code.

At the command line, run rake routes to confirm your events controller is set up correctly. you should see a line like this:

  event GET    /events/:id(.:format)      {:action=>"show", :controller=>"events"}

This indicates that the correct path to the show controller actually exists and the path to the controller should be event in your apps/views/event/index.html.erb code.

When do you get the error? When you are trying to display the events index route?

Upvotes: 0

Sandip Ransing
Sandip Ransing

Reputation: 7723

show page got wrong. there are 2 corrections - 1. @curevent needs to be @event 2. event_path should be events_path

<h1>SHOW EVENT DETAILS</h1>
<%= @event.name %>
<br/><br/>
<%= button_to "Back",events_path %>

Upvotes: 0

Baldrick
Baldrick

Reputation: 24340

It should be <%= button_to "Back",events_path %> (you're missing a 's') to go to the list of events.

event_path exists, it is used to show a particular event (the show action as stated in the error message, and you should give it an Event).

Upvotes: 1

drhenner
drhenner

Reputation: 2230

event_path 

should be

events_path

Upvotes: 0

gtd
gtd

Reputation: 17236

You want either events_path or event_path(@event) I believe.

Upvotes: 1

Related Questions