kid_drew
kid_drew

Reputation: 4005

Rails multiple create/update forms

Due to business logic, I want certain resources in my Rails app to be able to be created and updated from various forms. So for example, I have a resource Business, and depending on whether the user has just signed up or has been on the site for a while, a Business can be created from /account_setup (first time user) or /businesses/new (existing users).

I want to have both forms post to the same :create method and I want the method to be smart enough to figure out which page did the posting so it can render that page if it fails a validation. Just for clarity:

GET /account_setup => POST /businesses => businesses#create => {fail} => render 'account_setup'

GET /businesses_new => POST /businesses => businesses#create => {fail} => render 'new'

Since both are looking to create a Business, it's pointless to have two separate controller methods, and I'm assuming there's a Rails-y way to solve this. What's a good way to detect which of my two forms did the post?

Upvotes: 0

Views: 68

Answers (2)

Deb
Deb

Reputation: 1

You can put hidden fields in the form which will store the type of the form. Keep in mind, if it's a new user then you will have to create the user before creating business assuming the models are associated.

Upvotes: 0

tyamagu2
tyamagu2

Reputation: 969

Use request.original_fullpath in businesses#create or embed values specific to the pages(controllers) that show the forms using hidden_field.

Upvotes: 0

Related Questions