In the Rails 3 app I am working on, we allow users to “activate” an account (by typing in a coupon code) and then they type in their information and voila, their account is created.
I am running into a small issue that I am unclear how to solve without duplicating code (that doesn’t seem eloquent).
Here is my current activations controller:
class ActivationsController < ApplicationController
respond_to :html
skip_before_filter :set_client, :only => [:welcome]
def new
@user = User.new
@gc = get_group_code(params[:group_code])
if @gc.nil? || @gc.group.school_info.blank?
@countries = Country.all
@school_types = SchoolType.all
@states = State.all
else
@school_information = @gc.group.school_info
end
@user_type = UserType.find_by_type("Classroom Teacher")
end
def create
@user = User.new(params[:user].except(:custom))
if @user.save
@group = Group.find(params[:group_id])
@user.groups << @group
@user.update_mail_chimp_sponsors
UserMailer.welcome_email(@user, current_client).deliver
sign_in @user
redirect_to new_activation_welcome_path
else
render :new
end
end
def welcome
@user = current_user
end
private
def get_group_code(code = "")
group_code = GroupCode.find_by_code(code)
unless group_code.nil?
group_code.validate_code(current_client.name)
group_code
end
end
end
The problem I am running into, is on line 31 (render :new). When there is an error with the form and the app renders the new method, instance variables from the new method are not available to the view. How can I solve this issue?
Render :new does exactly that: it renders the :new view. It does not execute the new method in the controller, so in what ever method you call render :new from you must set up the instance variables required by the :new template.
It you do not want to duplicate code, you could move the setup of the instance variables to a private method in the controller. Then you can call the private method from the new method, and whatever other method from which you render :new.