Forum: Ruby on Rails Display form and process return values with same method?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cec8a9c67f25ebbaa7a802a16a409828?d=identicon&s=25 Peter Piper (Guest)
on 2006-01-23 11:36
Is it sensible to use the same method in a controller to both display a
form and process its return values?

So instead of new and create methods you would just have new.

Pseudo-code:

def new
  if system.post_response? then
    @user = User.create(params[:user])
    @user.save
  else
    @user = User.new
  end
end


Disclaimer: system.post_response? is completly made up! Basically i
guess it should return true if params[] has something in it.

Cheers Pete.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-01-23 14:40
(Received via mailing list)
Actual code (from a working controller):
   def add
     if request.get?
       @payer = Payer.new
     else
       @payer = Payer.new(params[:payer])
       if @payer.save
         flash[:notice] = "Payer #{@payer.name} created";
         redirect_to :action => 'add'
       end
     end
   end

You could also flip the clauses and ask "if request.post?" (not made
up!)

-Rob
This topic is locked and can not be replied to.