Search code examples
ruby-on-railsrubyconventions

What's the correct way to complete a Rails create action?


I have a User.create action that provisionally registers a new user and sends an email with a generated password. What is the most Rails-like way to complete that action? I want to do everything exactly right from now on. No more nonsense. This time I'm serious.

I'm thinking these are the options...

  1. Create a view called login_email_sent.html.haml and render it. (It would have a message like "thanks, an email has been sent with your login.")

  2. Create a view called create.html.haml and let Rails render that by default.

  3. Redirect the user to the same page as the form they just submitted with a message in the flash.

Or something else...?


Solution

  • This is more of a user experience question.

    Leave the create action as something that just creates and has no output (or just a simple 201 Created header if created by an API call). Do a redirect_to to either:

    1. A thanks page telling them how to wait and what will happen next
    2. The homepage with a very visible flash message letting them know what just happened.

    In 99.9% of cases you shouldn't ever need a template for create.

    def create
      User.create(...)
      respond_to do |format|
        format.html { redirect_to signup_complete_url }
        format.xml  { render :head => :created }
      end
    end
    

    For instance.