0

NOTE: I have read Routing From the Inside Out AND the Engine Yard blog post on routing.

I'm building a fantasy sports league, I have a League model that supports the seven basic restful operations, and they all work fine.

I've added the following my routes.rb

  resources :leagues do
    member do
      get :invite
      post :sendem
    end

Later in the file I have a resources :leagues for the basic actions

and when I rake routes I can see:

invite_league GET    /leagues/:id/invite(.:format)      {:action=>"invite", :controller=>"leagues"}
sendem_league POST   /leagues/:id/sendem(.:format)      {:action=>"sendem", :controller=>"leagues"}

which is what I would expect. I have two functions in the League controller: "invite" which creates the form for collecting email addresses, and "sendem" which invokes a mailer to actually send the invitations.

The get for /league/:id/invite does indeed produce the form. The form tag I am using looks like:

<%= form_tag(sendem_league_path, :method => "post") do %>

and yet the HTML that is rendered looks like:

<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">

And hence on submit generates a PUT which is completely wrong. (It should post to the sendem function.) My change to the routes file appears above the generic resources :leagues line, so it should have a higher priority.

I'm sure there is something dead-simple that I missed but I'm out of ideas. (And hair).

ScottyDont
  • 1,207
  • 1
  • 9
  • 17

2 Answers2

0

You should not use form_tag for manipulating resources. You should use form_for. Check out form helper guide - section 2 "Dealing with Model Objects". It takes care of deducing whether to use POST or PUT for a model object. For example, if your model object is new, it will use post on "resources"'s URL. if it is already existing database entity, it will use PUT to that "resource"'s URL.

Salil
  • 9,534
  • 9
  • 42
  • 56
  • I already have CRUD functions for this resource -- these are additional functions that at most read from the model's attributes (nothing is written, created, or updated). I also tried the approach of moving these functions into a separate controller, but that approach didn't work either. – ScottyDont Jun 17 '12 at 13:43
0

ARGH some form handling error code at the top (form for @league) created a second form on the page for editing.... (left out of code snippets above for brevity). Original code seems to work as expected with that other code commented out. Thanks to vladdruzh for convincing me I was on the right track and to Salil for making me think to read the rendered HTML top to bottom.

ScottyDont
  • 1,207
  • 1
  • 9
  • 17