I'm a newbie in hanami coming from a rails background, and I would like to know the best practice in verifying that a model has been persisted in a create Action.
I created a users/create action that looks like this:
class Create
include Web::Action
def call(params)
repository = UserRepository.new
@user = repository.create(name: params[:user][:name],
email: params[:user][:email],
type: 'standard')
redirect_to '/users'
end
end
I wanted to make sure that I only redirect the users if the user was successfully created. I rails, I would do something like this:
redirect_to '/users' if @user.persisted?
But that is not the hanami way of doing things. Currently I'm doing this:
redirect_to '/users' if [email protected]?
Which does the job, but it doesn't looks clean. How would be the best way to do this with hanami?
Try something like this.
def call(params)
repository = UserRepository.new
@user = repository.create(name: params[:user][:name],
email: params[:user][:email],
type: 'standard')
redirect_to '/users'
rescue Hanami::Model::Error
# handle the error
end