Passing Params With Rails Render AKA Lesson on Rails Render vs Redirect_to

Problem: User is directed to the purchase page of the website with a discount code
url/discount_code=trial5&product=greatest-product
While purchasing user forgets to add password, hit submit button
Rails(with Devise gem) sends user to url/users but now discount is gone!

This bug … well…bugged me for a morning. I hope this post helps others with the same problem.

This is how my RegistrationsController which inherits from Devise looked

  def new
    @user = User.new
    @purchase = Purchase.new
    super
  end

  def create
    @user = User.new(user_params)
    if @user.save
       redirect_to desired_path
    else
      @purchase = Purchase.new
      return render 'new'
    end
  end
 

I figured what I needed was to send the discount params to render ‘new’. How could I could that. First let me tell you the difference from redirect_to and render.

Render
We can give render the name of an action to cause the corresponding view template to be rendered.

For instance, if you’re in an action named update, Rails will be attempting to display an update.html.erb view template. If you wanted it to display the edit form, associated with the edit action, then render can override the template selection.

When render :edit is executed it only causes the edit.html.erb view template to be displayed. The actual edit action in the controller will not be executed.

Redirect_to

Redirect_to creates a new http request. When a user submits data it comes in as a POST request. If we successfully process that data we likely next display them the data they just created. We could display the article using render in the same POST that sent us the data. But it is not the Rails way. If the user hit the back arrow on the browser it will prompt the User to submit form data again?! when you successfully store data you want to respond with an HTML redirect. That will force the browser to start a new request.
That’s why in the create method in the registrations controller I want to redirect if @user is successfully saved. In this example, the application saves user if new user buys a product.

The Solution

After this brief lesson on the difference between render and redirect. The solution to persist discount when the new.html.erb page is rendered by loading the discount object before rendering the action.

  def new
    @user = User.new
    @purchase = Purchase.new
    super
  end

  def create
    @user = User.new(user_params)
    if @user.save
       redirect_to desired_path
    else
      @purchase = Purchase.new
      @discount = Discount.find_by(code: params[:purchase][:discount_code])
      return render 'new'
    end
  end
 

The params[:purchase][:discount_code] was being passed to the render new action but what I just needed was to load define @discount with the Discount object so my new.html.erb knew what to do with it.

Debugging is a skill that comes with time and good understanding of the framework and tools you are using to develop software. Slowly but surely, we are all on our way there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s