A question about the RESTful Routing

Hello!

Thank you for reading my post, if I am not being careful about the use
of terminology or am not being exactly clear enough for my question to
be understood, kindly notify and I will correct.

I remember that when I used Rails 1.2, I can just add a blank action
in a controller and then create a .rhtml with that same name as does
the action and then I can just write "hello world"in that .rhtml and
it will show in
http://localhost:3000/controller/thatNewActionIJustWrote/

Now, I go back to my bookstore controller and add
def greenapple
end

and I go create greenapple.html.erb
and I typed “hello world” in greenapple.html.erb (I know it’s silly, I
just wanted to demonstrate what my problem is)

It gives me:
Routing Error
No route matches “/greenapple/” with {:method => :get}

Any ideas? Or, which I am sure, if you know some web pages that I can
read through, that will be great, too!!

Thanks a quintillion!

Nik

Hello all!

I am not sure if I have solved this, maybe I shouldn’t trying things
out at 1am.

But here’s what I got
I added one line
map.connect
‘greenapple’, :controller=>‘bookstore’, :action=>‘greenapple’
in the config/routes.rb

And it worked!

Can I change my question to:
If I am to add a new action in a controller, do I always have to
manually specify a map.connect ‘name of that new action’, :controller
=> ‘name of that controller’, :action=>‘name of that action again’
?

If so, how come this is the case whereas the old rails needs not? If
not, what’s the advantage?

Thank You all! Have a great day!

Nik wrote:

If I am to add a new action in a controller, do I always have to
manually specify a map.connect ‘name of that new action’, :controller
=> ‘name of that controller’, :action=>‘name of that action again’
?

If so, how come this is the case whereas the old rails needs not? If
not, what’s the advantage?

In Rails 1.2 if you are not using RESTful routes, then you will just
have, by default, routing lines like this:

map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’

With Rails2 using RESTful routes, then you will probably have a line
like this before those (if they are present):

map.resources :bookstore

The documentation will give you the full details about this:

http://api.rubyonrails.com/classes/ActionController/Resources.html#M000308

In particular, if you want to add a non-REST method to the controller
you can change the resources line to:

If the #greenapple method is expecting an :id parameter
(/bookstore/greenapple/1 etc):
map.resources :bookstore, :member => {:greenapple => :get}

If the #greenapple method does not expect an :id but is an operation on
all items controlled by the bookstore controller (/bookstore/greenapple
on its own):
map.resources :bookstore, :collection => {:greenapple => :get}

If the method is the result of a form post then replace :get with :post.
The documentation has numberous examples.

In any event, with a Rails2 application, run the following at the top
level of the application:

rake routes

This will show you how any given URL will get converted to
controller/action and when changing the routes.rb file, if you are
unsure what effect your are having, just run the rake command again to
see the difference.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs