I am new to Ruby on Rails, I have completed the Blog Tutorial.
I am now trying to add an additional action to the controller, called ‘start’.
def start end
I have added a view page ‘app/views/posts/start.html.erb’ containing nothing but simple html.
When I go to /posts/start i get the following error.
ActiveRecord::RecordNotFound in PostsController#show Couldn't find Post with ID=start
I understand the error, the show action is being executed and start is not a valid ID. Why doesn’t the start action get executed, is there some part of the MVC architecture or configuration I am missing ?
Below is my posts_controller.rb
class PostsController < ApplicationController # GET /posts/start def start end # GET /posts # GET /posts.xml def index @posts = Post.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @posts } end end # GET /posts/1 # GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } end end end
Yes I have restarted the server and tried it with Mongrel and webrick.
Your routing isn’t set up to allow that route. Assuming you’re using the default scaffolding, put this line before the
map.resources :postsline in config/routes.rb:The regex for
:actionrestricts it to just a-z (to avoid catching things like /posts/1). It can be improved if you need underscores or numbers in your new actions.