pthorsson
pthorsson

Reputation: 341

Route same match to multiple controllers?

I got two forms linked to different actions on the same page. When one form failed I still want the same url in the address field, and that goes for both forms. My routes looks like this, but the first one is overriding the second or something like that.

match "send-clientletter" => "mail_lists#compose_clientletter", :as => "compose_clientletter", via: :get
match "send-clientletter" => "mail_lists#send_clientletter", :as => "send_clientletter", via: :post
match "send-clientletter" => "mail_lists#client_create", :as => "client_create", via: :post

As you can see I got two via: :post and that's my problem here.. Since I still want the same url for both form, so I can do render "compose_clientletter" if theres any errors in any of the forms.

Is there any way to do this? Or do I have to live with two different urls if the forms fails?

Upvotes: 1

Views: 169

Answers (1)

Brad Werth
Brad Werth

Reputation: 17647

Well not technically RESTful, you could switch one to use PUT and one to use POST. It doesn't sound like this is strictly RESTful, anyway, so that's probably not an issue...

An alternate, possibly cleaner, way to accomplish the same thing would be to just make them go to the same action, which might look something like this:

def my_action
  if params[:object_1]
    method_to_handle_object_1_creation
  elsif params[:object_2]
    method_to_handle_object_2_creation
  end
end

Upvotes: 1

Related Questions