views:

27

answers:

2

Is it possible to have a controller action do both the GET and POST?

i.e. the GET shows a form, and the POST takes the forms values and saves to the db.

A: 

Yes it's possible. You just need check the method to call you action

  def show

    if request.post?
      render :text => "it's a post"
    elsif request.get?
      render :text => "it's a get"
    else
      render :text => "it's another method"
    end
  end
shingara
It's possible, but don't do this unless you want to learn bad habits and write confusing code.
Beerlington
+6  A: 

As already mentioned it is possible, but I feel it is bad style. Showing a form and saving something are different actions and your code should reflect that.

If you just want to access both action via the same url you can just set up your routes accordingly. This is done differently depending on whether you use Rails 2 or Rails 3.

fly.floh
Totally agree. Split methods, and call them using routes. All in one is very bad approach, totally impossible to support.
Dmitry Polushkin
+1. Prefer RESTful routing: http://guides.rubyonrails.org/routing.html
Dave Sims