Topic: restul routing and named route methods

i'm new to webdevelopment, rails and ruby.

i'm somewhat confused about named route methods that are generated with restful routing

say when i do

map.resources :posts

i realize that rails generates a lot of named routes which i can use as {named_route}_path and {named_route}_url. what i can't figure out is how do i know what parameters those named route methods take? would anybody be kind enough to help me out with this?

also how can i find out which parameters does resources method takes. i recently saw the following pieces of code but was not able to decipher them since i wasn't able to find out what parameters this method takes

map.resources :posts, :member => {:retract => :get}
map.resources :posts, :collection => {:showall => :any}

any help is appreciated

Re: restul routing and named route methods

The :member and :collection and :new hashes don't introduce any new parameters to your actions.

They add new routes, to new actions that you've written.  Remember that rails  resource oriented (RESTful) routing only introduces ONE argument , the ubiquitous :id, or with nested routes, <model>_id.  Any OTHER parameter in your routes must be put there by YOU!

Take the :member example you posted

Before you added that line to your routes,  you only had ONE way to update a post

PUT  /posts/1   will call action update

after you added that line,  you have a new SPECIAL way to update that post

GET /posts/1;retract   will call action retract

Now both the update and retract actions will start off the same,  dealing with the id parameter,  but do different things with that id

def update
  @post = Posts.find(params[:id])
  ... classic update
end

def retract
  @post = Posts.find(params[:id])
  @post.status = 'retracted'
  @post.save!
end

Now that said,  I'd say the example you posted is probably not correct,   to retract a post,  you have to make a change to the resource (you have to change a value in the post record, or delete it, whatever).  I think that should be done with a PUT,  because it changes the resource.  So I'd so this instead:

map.resources :posts, :member => {:retract => :put}

Either way will work,  but using :put instead of :get is more RESTful.

Last edited by BradHodges (2010-12-21 20:42:42)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: restul routing and named route methods

BradHodges wrote:

def update
  @post = Posts.find(params[:id])
  ... classic update
end

def retract
  @post = Posts.find(params[:id])
  @post.status = 'retracted'
  @post.save!
end


thanks for the previous reply. things are getting a little more clearer but not quite.  i got more questions.


will

map.resources :post, :member => {:retract =>:get}

always map to

def retract
  @post = Posts.find(params[:id])
  @post.status = 'retracted'
  @post.save!
end

or it can map to something else? or more specifically, how do you know that it will map to this code and not something else?

also i have never seen a request in the following format

GET /posts/1;retract

specifically

;retract

shouldn't it look similar to this?

GET /posts/1 name=MyName&last_name=MyLastName

is this something specific to rails or it is just the format i have never seen before?

any help is appreciated.

Re: restul routing and named route methods

map.resources :post, :member => {:retract =>:get}

Just adds the route,  YOU have to write the retract action,  Rails assumes you've got a retract action defined,  because you put it in your routes.  It can do whatever you want.  The only thing for sure is that it's params[:id] will contain the id you want to retract.

You could do this if you wanted.

def  retract
  FileUtils.rm_f(Rails.root.to_s)
end

You got passed the id of the Post to be retracted,  but you decided to ignore it and blast your site into oblivion!!!   Dumb example,  but you get the point.

the semicolon can be used in a URI, similar to the &;

If you want an example,  just issue the following command:

rake routes

You'll see the default restful routes use ;edit appended to the URI to implement the edit RESTFul route.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: restul routing and named route methods

thanks for the help. i think i'm starting to get it. however i still don't understand how to determine from the rake routes command what parameters are passed to the named route methods?

below is an example i got from the rake routes command. how would i know what parameters i need to pass to say, the formatted_edit_plugin_path, or formatted_edit_plugin_url methods?

help is very much appreciated

                     plugins GET    /plugins                                     {:action=>"index", :controller=>"plugins"}
           formatted_plugins GET    /plugins.:format                             {:action=>"index", :controller=>"plugins"}
                             POST   /plugins                                     {:action=>"create", :controller=>"plugins"}
                             POST   /plugins.:format                             {:action=>"create", :controller=>"plugins"}
                  new_plugin GET    /plugins/new                                 {:action=>"new", :controller=>"plugins"}
        formatted_new_plugin GET    /plugins/new.:format                         {:action=>"new", :controller=>"plugins"}
                 edit_plugin GET    /plugins/:id/edit                            {:action=>"edit", :controller=>"plugins"}
       formatted_edit_plugin GET    /plugins/:id/edit.:format                    {:action=>"edit", :controller=>"plugins"}
                      plugin GET    /plugins/:id                                 {:action=>"show", :controller=>"plugins"}
            formatted_plugin GET    /plugins/:id.:format                         {:action=>"show", :controller=>"plugins"}
                             PUT    /plugins/:id                                 {:action=>"update", :controller=>"plugins"}
                             PUT    /plugins/:id.:format                         {:action=>"update", :controller=>"plugins"}
                             DELETE /plugins/:id                                 {:action=>"destroy", :controller=>"plugins"}
                             DELETE /plugins/:id.:format                         {:action=>"destroy", :controller=>"plugins"}
              plugin_ratings GET    /plugins/:plugin_id/ratings                  {:action=>"index", :controller=>"ratings"}
    formatted_plugin_ratings GET    /plugins/:plugin_id/ratings.:format          {:action=>"index", :controller=>"ratings"}
                             POST   /plugins/:plugin_id/ratings                  {:action=>"create", :controller=>"ratings"}
                             POST   /plugins/:plugin_id/ratings.:format          {:action=>"create", :controller=>"ratings"}
           new_plugin_rating GET    /plugins/:plugin_id/ratings/new              {:action=>"new", :controller=>"ratings"}
 formatted_new_plugin_rating GET    /plugins/:plugin_id/ratings/new.:format      {:action=>"new", :controller=>"ratings"}
          edit_plugin_rating GET    /plugins/:plugin_id/ratings/:id/edit         {:action=>"edit", :controller=>"ratings"}
formatted_edit_plugin_rating GET    /plugins/:plugin_id/ratings/:id/edit.:format {:action=>"edit", :controller=>"ratings"}
               plugin_rating GET    /plugins/:plugin_id/ratings/:id              {:action=>"show", :controller=>"ratings"}
     formatted_plugin_rating GET    /plugins/:plugin_id/ratings/:id.:format      {:action=>"show", :controller=>"ratings"}
                             PUT    /plugins/:plugin_id/ratings/:id              {:action=>"update", :controller=>"ratings"}
                             PUT    /plugins/:plugin_id/ratings/:id.:format      {:action=>"update", :controller=>"ratings"}
                             DELETE /plugins/:plugin_id/ratings/:id              {:action=>"destroy", :controller=>"ratings"}
                             DELETE /plugins/:plugin_id/ratings/:id.:format      {:action=>"destroy", :controller=>"ratings"}
                  categories GET    /categories                                  {:action=>"index", :controller=>"categories"}
        formatted_categories GET    /categories.:format                          {:action=>"index", :controller=>"categories"}
                             POST   /categories                                  {:action=>"create", :controller=>"categories"}
                             POST   /categories.:format                          {:action=>"create", :controller=>"categories"}
                new_category GET    /categories/new                              {:action=>"new", :controller=>"categories"}
      formatted_new_category GET    /categories/new.:format                      {:action=>"new", :controller=>"categories"}
               edit_category GET    /categories/:id/edit                         {:action=>"edit", :controller=>"categories"}
     formatted_edit_category GET    /categories/:id/edit.:format                 {:action=>"edit", :controller=>"categories"}
                    category GET    /categories/:id                              {:action=>"show", :controller=>"categories"}
          formatted_category GET    /categories/:id.:format                      {:action=>"show", :controller=>"categories"}
                             PUT    /categories/:id                              {:action=>"update", :controller=>"categories"}
                             PUT    /categories/:id.:format                      {:action=>"update", :controller=>"categories"}
                             DELETE /categories/:id                              {:action=>"destroy", :controller=>"categories"}
                             DELETE /categories/:id.:format                      {:action=>"destroy", :controller=>"categories"}
                        root        /                                            {:action=>"index", :controller=>"plugins"}
                                    /:controller/:action/:id                     
                                    /:controller/:action/:id.:format             

Re: restul routing and named route methods

The COLON!

If you see a colon in the route definition,  it means that part of the URI is passed as an argument!

But, before I go any further,  understand this explanation isn't really needed if you just GO WITH THE FLOW and let the URL helpers HELP you.


formatted_edit_plugin_rating GET    /plugins/:plugin_id/ratings/:id/edit.:format {:action=>"edit", :controller=>"ratings"}


if I send the app this URI

/plugins/4/ratings/33/edit

Then the edit action in ratings_controller.rb is called

def edit
  @pid = params[:plugin_id]
  @id = params[:id]
def

Then in app/views/ratings/edit.rhtml

<% = "You expect to edit rating with an id of #{@id},  which I'm told belongs to the plugin who's id is #{@pid}" %>

Last edited by BradHodges (2010-12-21 20:38:47)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: restul routing and named route methods

thanks for the help. i really appreciate it.