1
votes

Hi i created a controller article and added the following code

def chid
  @message='hello world'
    respond_to do |format|
    format.html
    end

end

I then created

chid.html.erb
file in
app/views/articles/

and wrote the following code

Hello world

When i gave the following url

../articles/chid 
i dunnot get any output.. am i missing something?

My log portion

Processing ArticlesController#show (for 127.0.0.1 at 2011-01-12 21:51:01)
 [GET] Session ID: BAh7BzoMY3NyZl9pZCIlMTA0ZWY2ZTUzYjQxZGJkZmFlMTQwNWRjYjczNTRm%0AODAiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D--de7737601817f52c1b72daca6061c5126f3a5022
 Parameters: {"action"=>"show", "id"=>"chid", "controller"=>"articles"}
 Rendering template within layouts/articles 
Rendering articles/show Completed in 0.01000 (100 reqs/sec) | Rendering: 0.00600 (60%) | DB: 0.00000 (0%) | 200 OK [localhost/articles/chid/]

my router.rb file is as follows


ActionController::Routing::Routes.draw do |map|
  map.resources :articles
map.connect ':controller/:action/:id'
   map.connect ':controller/:action/:id.:format'
   map.match ':controller/:action/' => 'Article#chid'
 end
1
@Matheus: No. wat should i do in there? sorry am jus learning RORCHID
@Matheus: The page is being correctly rendered.. But there s no outputCHID
@jdl. is this what u asked? Processing ArticlesController#show (for 127.0.0.1 at 2011-01-12 21:51:01) [GET] Session ID: BAh7BzoMY3NyZl9pZCIlMTA0ZWY2ZTUzYjQxZGJkZmFlMTQwNWRjYjczNTRm%0AODAiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D--de7737601817f52c1b72daca6061c5126f3a5022 Parameters: {"action"=>"show", "id"=>"chid", "controller"=>"articles"} Rendering template within layouts/articles Rendering articles/show Completed in 0.01000 (100 reqs/sec) | Rendering: 0.00600 (60%) | DB: 0.00000 (0%) | 200 OK [localhost/articles/chid/]CHID
The server process it as though , the show action was invoked with id chid.. it doesnot call the action chid.. this s what i understood from logs. @jdlCHID
You need to add a special case to your routes to match the chid action since it is not a CRUD operation. In this case, it is interpreting chid as the id of the article and attempting to show on it.Matheus Moreira

1 Answers

2
votes

In the config directory there is a file named routes.rb. This file tells Rails how to respond to certain URLs by mapping them a controller's action.

Consider the example:

match '/articles/chid' => 'Articles#chid'

This will route #{your_site_url}/articles/chid to ArticlesController's chid action, which in turn would render the chid.html.erb view located in views/articles.

We can also tell Rails what to route / to as well:

root :to => 'Articles#chid'

And, finally, we could also route any controller to any action using what's called Bound Parameters:

match ':controller/:action'

As a last note, definitely check out the Rails Guides on Routing.

Update:

Try using the following routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action'
end

Edit

Based on the routes.rb file you just posted, this line is the culprit. Delete it.

map.connect ':controller/:action/:id'

Note that the routes match in order from the top of the file to the bottom and once it finds a match, it's done. It won't look at the rest of your routes.