Probably common n00b question: Default id

Hi, I’m new to Rails but I develop in PHP somtimes. I figured that the
best way to learn Rails is to create a simple CMS. While I understand
the basics of ruby and rails, one issue has caught me: how can I set a
default ID for an action?

Here is my code for the index action in my pages controller:

def index

@page = Page.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @page }
end

end

However, when I load http://localhost:3000/pages , I get a message:

Couldn’t find Page without an ID

How can I make my application assume a default ID of 1 if none is
provided?

On Mar 6, 2008, at 12:12 PM, Tom S. wrote:

How can I make my application assume a default ID of 1 if none is
provided?

I’m not sure that’s what you want to do. In Rails 2.0.x, the mapping is:

GET => action => index => list of items
GET /id => action => show => individual item identified by id

Run rake routes to see how your HTTP verbs map to your controller/
action pairs.

Maybe I should look at some other examples

Yeah, that’s good, thanks!

Tom S. wrote:

Maybe I should look at some other examples

def pages
if params[:id]
@hotel = Hotel.find(params[:id])
else
@hotel = Hotel.find(:all)
end

end

HOpe it can help you.

Reinhart Ariando
WEB: Teapoci.Blogspot.com
YM : Booking2Heaven
MSN: [email protected]