Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an action to an existing controller (Ruby on Rails)

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.

like image 345
Mark Avatar asked Dec 30 '08 19:12

Mark


People also ask

How do I add a action in Rails?

When you click the Submit button in your form, it posts your data to the create action, which actually creates the record in the database. to your create action, since that is where you're actually saving the post to the database. ok,that means create is method which will be auto called at submit button !!

What is action controller Rails?

Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.

What is Before_filter in Ruby on Rails?

Before Filters ADVERTISEMENT. ADVERTISEMENT. Rails before filters are executed before the code in action controller is executed. The before filters are defined at the top of a controller class that calls them. To set it up, you need to call before_filter method.

What are actions in Ruby on Rails?

Ruby on Rails defines seven standard controller actions can be used to do common things such as display and modify data. If you only want to create routes for specific actions, you can use :only to fine tune the resource route. This line maps URLs only to the Message controller's index and show actions.


1 Answers

The error you're making is actually a pretty common one.

Basically, Rails automatically maps URLs for your scaffolds. So when you created the Posts scaffolds, Rails is mapping the URL routes for it. One such route is the URL for viewing a single post: /posts/(post_id)

So, when you're entering the URL /posts/start Rails thinks you're saying "Hey, give me the post with ID = start. So Rails complains that the show method can't find a post with such ID.

One quick way to fix this is to make sure your config/routes.rb has the route for the start action before the scaffolding routes:

# Route for start action map.connect '/posts/start', :controller => 'posts', :action => 'start' # Default mapping of routes for the scaffold map.resources :posts 

Anyway, hope that helps.

like image 64
David Avatar answered Sep 20 '22 02:09

David