I’m trying to find a the best way of creating an account and a user at
the same time (the restful way).
When a user wants to register, he needs to create an account first. So
he gives his account info, at the bottom of the form, he can insert his
user details.
But how do you catch this in the controllers? First the account need to
be created, so you submit everything to the AccountController where you
create the account, but then the user needs to be created. So the user
info should be given to the user controller that creats the User.
I could create them both in the Account controller, but that doesn’t
seem so REST.
Before I forget, an Account can have many users. That’s why I’m doing it
like this.
Another possibility would be to split the form into 2 seperate forms, so
that first the account needs te be created and then a seperate user form
where the user gets created.
Some of you who have a better solution for this “problem”?
We’re doing it exactly the same way, in this case the Accounts
controller has to do the job.
Split the form as you mentioned, then you shoiuld get two params, one
params[:account], one params[:user]
in the controller you have a few options to handle this, either first
create the account and if save works without errors, create the user.
second option would be to create (but not save) both objects, then
validate them. Advantage here would be, that you can show all validation
errors to the user if something goes wrong here.
Another possibility would be to split the form into 2 seperate forms, so
that first the account needs te be created and then a seperate user form
where the user gets created.
True, from a programming view that would be the best solution. But users
have a nasty habit to complain if they don’t understand the reason for
two steps if they think it could be done in one step (ok, they’re only
users, but: they pay…)
So everything happens in you AccountController. Something like this:
def create
# create_account is in Account model
account = Account.create_account(params[:account])
if account.save
#create_admin is in User model
user = User.create_admin(account, params[:user])
if user.save
redirect_to(resources_url())
end
end
end
Could be the solution untill I can find a nicer way of handling it. Thx
for the info
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.