How does Rails link_to connects to the show.rhtml?

Environment: rails 2.0.2 on OS X.

I wish I had a better way of showing this…
I have a question about the logic path hidden within Rails 2.0.2.

Scenario:

  1. I’m viewing a partial view and click on a linked line of an item of a
    collection.
  2. I’m transferred to the particular item’s SHOW action (show.rhtml).
  3. How does Rails know (how do I follow the path) to execute the SHOW
    within the controller?
  4. Is ‘SHOW’ action a Rails reserve word?

The following are snippets of the ‘relevant’ files of the project:

routes.rb:
map.resources :chapters do |chapters|
chapters.resources :revisions, :comments
end


chapters_controller.rb:
class ChaptersController < ApplicationController

GET /chapters

GET /chapters.xml

def index <----------- (1) displays the
partial ‘_chapter.rhtml’ via index.rhtml
@chapters = Chapter.find_ascending

respond_to do |format|
  format.html # index.rhtml
  format.xml  { render :xml => @chapters.to_xml }
end

end

#… Question: how does ‘index’ render ‘show’?

GET /chapters/1

GET /chapters/1.xml

def show
@chapter = Chapter.find(params[:id])

respond_to do |format|
  format.html # show.rhtml
  format.xml  { render :xml => @chapter.to_xml }
end

end


end

index.rhtml:

    <%= render :partial => @chapters %> <%# Rails 2 %>

_chapter.rhtml:
<%= link_to chapter.title, chapter %> <---- user *** clicks *** on
Chapter.title

========

… control passes to the show.rhtml. Why?

Rails path question:

  1. User clicks on chapter.title display within partial _chapter.rhtml.
  2. Control bounces over to ‘show.rhtml’. <-- how does it do it?

Note: It has something to do with routes.rb.


Regards,

Ric.

Frederick L. wrote:

Note: It has something to do with routes.rb.

Regards,

Ric.

You are right!

go to http://www.b-simple.de/documents and download the free pdf on
restful rails.

Restful rails (i.e. map.resources) does many tricks for you and I hope
this document will give you more insight into the magic that goes on
behind it.

hth

ilan

_chapter.rhtml:
<%= link_to chapter.title, chapter %> <---- user *** clicks *** on
Chapter.title

… control passes to the show.rhtml. Why?
Rails path question:

  1. User clicks on chapter.title display within partial _chapter.rhtml.
  2. Control bounces over to ‘show.rhtml’. ← how does it do it?
    Note: It has something to do with routes.rb.

First of all ignore that fact that you are in a partial. That may
complicate the issue for you. Take a closer look at the link_to. The
object passed to link_to is a single Chapter object. Now, think about
the route.rb file. You have asked Rails to generate a set of RESTful
routes. You can see examples of those routes by running “rake routes.”
Since the Chapter object you passed into link_to is a singe, existing
(that is chaper.new_record? returns false), there’s only one logical
place to go. That, of course, is chapters/show.html.erb (show.rhtml
← old and tired file name).

Now, besides that there is more magic going on under the cover. What
happens if the mime-type of the request is not text/html but rather
some other type like application/atom+xml (which would be mapped to a
symbol like :atom? You can see more detail about that by looking for
rails multi-view). What happens in this case is that Rails may look
for a file something like show.atom.builder. This would then use the
builder (formerly identified by show.rxml) to return the atom feed
instead of the HTML view.

Pretty cool eh? All you need to do to get the atom feed is just use
URLs like chapters/1.atom. If everything is set up correctly all you
need to do in your controller to support this multi-view is add atom
to your respond_to block.

Also by taking advantage of the new template naming scheme in Rails
2.x you can even have multiple views of the same format. Say you
wanted to build an iPhone optimized view, which would also be using
HTML (ERB). Now in Rails 2.x you can have both show.html.erb and
show.iphone.erb. Both are HTML views representing the same content but
formatted for different platforms.

Hope that helps clear things up a bit.
On Mar 6, 10:52 pm, Ilan B. [email protected]

Ilan B. wrote:

You are right!

go to http://www.b-simple.de/documents and download the free pdf on
restful rails.

Restful rails (i.e. map.resources) does many tricks for you and I hope
this document will give you more insight into the magic that goes on
behind it.

hth

ilan

Robert W. wrote:

_chapter.rhtml:
<%= link_to chapter.title, chapter %> <---- user *** clicks *** on
Chapter.title

… control passes to the show.rhtml. Why?
Rails path question:

  1. User clicks on chapter.title display within partial _chapter.rhtml.
  2. Control bounces over to ‘show.rhtml’. ← how does it do it?
    Note: It has something to do with routes.rb.

First of all ignore that fact that you are in a partial. That may
complicate the issue for you. Take a closer look at the link_to. The
object passed to link_to is a single Chapter object. Now, think about
the route.rb file. You have asked Rails to generate a set of RESTful
routes. You can see examples of those routes by running “rake routes.”
Since the Chapter object you passed into link_to is a singe, existing
(that is chaper.new_record? returns false), there’s only one logical
place to go. That, of course, is chapters/show.html.erb (show.rhtml
← old and tired file name).

Hope that helps clear things up a bit.
On Mar 6, 10:52�pm, Ilan B. [email protected]

Wow! Awesome!
My single-dimensional brain hurts!

I did the following per your suggestion (abridged):

$>rake routes
(in /Users/Ric/workarea/ruby/books)

chapters GET /chapters {:action=>“index”, :controller=>“chapters”}

chapter GET /chapters/:id {:action=>“show”, :controller=>“chapters”}

PUT /chapters/:id {:action=>“update”, :controller=>“chapters”}
chapter_revision GET /chapters/:chapter_id/revisions/:id
{:action=>“show”, :controller=>“revisions”}
chapter_comment GET /chapters/:chapter_id/comments/:id
{:action=>“show”, :controller=>“comments”}

Please forgive a neophyte’s major paradigm shift:
<<< zoom, swish, bang…>>>

Thanks for the pointing me towards the light!

Regards,

Ric.