Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render_wizard callback #277

Open
RobertAudi opened this issue Apr 21, 2021 · 3 comments
Open

render_wizard callback #277

RobertAudi opened this issue Apr 21, 2021 · 3 comments

Comments

@RobertAudi
Copy link
Contributor

When the render_wizard method is called with a resource it saves it, and then either redirects to the next step or re-renders the current step in case of errors.

Looking at the code, it is impossible to perform actions based on the result of the save. One such example is flash messages, but this can be worked around using the options argument of render_wizard.

Another example that is impossible to solve at the moment is business logic after saving the resource.

At this point I don't know if this is a feature request or a question on how to solve this problem, but I would be surprised if I am the first person to bring this up. Is there a known solution or workaround to this problem?

@jonsgreen
Copy link

@RobertAudi I had the same question today. We are solving the issue right now by doing the update first, performing our business logic and then calling render_wizard. However, this is not exactly ideal since it is saving the record twice. It does seem like it would be nice to have callbacks at least for the success case when calling render_wizard.

@mattantonelli
Copy link

I'm running into the same issue trying to render a flash based on whether or not the update was successful. The sample code from the README is also calling @user.update followed by render_wizard which then executes a redundant save. This means that validations will always be called twice on each step, which I'd really like to avoid if possible.

@mattantonelli
Copy link

I've come up with the following solution. In the event of an error, you can call render_step to re-render the current step (e.g. with your errors displayed to the user) and skip the extra save call.

def update
  @model.update(model_params)

  if @model.errors.any?
    # TODO: Your business logic goes here

    render_step(step)
  else
    render_wizard(@model)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants