title: Ruby on rails controller code, needs refactor best way to approach for more dry?
body: <p>I have a welcome wizzard that builds a user profile when first login.<em>Problem is it is quite messy implemented but I have tried to refactor it several times and rewrite it but cannot comeup with something better then below.</em></p>
  
  <p>In ideal world it would be all inside welcome_controller.rb but this have caused much headache so Now i rewrote the update method for profile_controller instead. </p>
  
  <p><strong>Any thoughts on how to improve this make it more dry and clean?</strong> Would love to recieve some good input on this and thoughts perhaps to move all update stuff to welcome controller instead?</p>
  
  <p>WelcomeController:</p>
  
  <pre><code>class WelcomeController &lt; ApplicationController
  
    before_filter :authenticate_user!
    before_filter :load_step
  
    layout "welcome"
  
    def sub_layout
     "center"
    end
  
    def edit
      # form updates post to edit since
      # profile is non existant yet
  
      params[:step] = "photos" unless params[:step]
  
      @photos   = Photo.where(:attachable_id =&gt; current_user.id)
      @profile  = Profile.where(:user_id =&gt; current_user.id).first
      @photo    = Photo.new
  
      if ["photos", "basics", "details", "test"].member?(params[:step])
  
        # force rendering the correct step
        case current_user.profile.step
          when 1
            render :template =&gt; "/profiles/edit/edit_photos", :layout =&gt; "welcome"
          when 2
            render :template =&gt; "/profiles/edit/edit_basics", :layout =&gt; "welcome"
          when 3
            render :template =&gt; "/profiles/edit/edit_details", :layout =&gt; "welcome"
          when 4
            render :template =&gt; "/profiles/edit/edit_test", :layout =&gt; "welcome"
        end
  
      else
        render :action =&gt; "/profiles/edit/edit_photos"
      end
    end
  
    def load_step
  
      redirect_to root_path if current_user.profile.complete
  
      case current_user.profile.step
        when 1
          redirect_to "/welcome" unless params[:controller] == "welcome"
        when 2
          redirect_to "/welcome/basics" unless params[:controller] == "welcome" &amp;&amp; params[:action] == "edit" &amp;&amp; params[:step] == "basics"
        when 3
          redirect_to "/welcome/details" unless params[:controller] == "welcome" &amp;&amp; params[:action] == "edit" &amp;&amp; params[:step] == "details"
        when 4
          redirect_to "/welcome/test" unless params[:controller] == "welcome" &amp;&amp; params[:action] == "edit" &amp;&amp; params[:step] == "test"
      end
    end
  
  end
  </code></pre>
  
  <p>ProfileController:</p>
  
  <pre><code>class ProfileController &lt; ApplicationController
  
  ...
   def update
      @profile        = Profile.find(params[:id])
      @tags           = Session.tag_counts_on(:tags)
      @profile.form   = params[:form]
      @match          = Match.where(:user_id =&gt; current_user.id).first
      authorize! :update, @profile
  
      respond_to do |format|
  
        if @profile.update_attributes(params[:profile])
  
          if current_user.profile.complete
            format.html { redirect_to "/profiles/#{ current_user.username }/edit/#{ @profile.form }", notice: t('notice.saved') }
          else
            case current_user.profile.step
              when 1
                current_user.profile.update_attributes(:step =&gt; 2)
                format.html { redirect_to "/welcome/basics", notice: t('notice.saved') }
              when 2
                current_user.profile.update_attributes(:step =&gt; 3)
                format.html { redirect_to "/welcome/details", notice: t('notice.saved') }
              when 3
                current_user.profile.update_attributes(:step =&gt; 4)
                format.html { redirect_to "/welcome/test", notice: t('notice.saved') }
            end
          end
  
        else
  
          if current_user.profile.complete
            format.html { render action: "/edit/edit_" + params[:profile][:form], :what =&gt; @profile.form }
          else
            case current_user.profile.step
              when 1
                current_user.profile.update_attributes(:step =&gt; 2)
                format.html { redirect_to "/welcome/basics", notice: t('notice.saved') }
              when 2
                current_user.profile.update_attributes(:step =&gt; 3)
                format.html { redirect_to "/welcome/details", notice: t('notice.saved') }
              when 3
                current_user.profile.update_attributes(:complete =&gt; 1)
                format.html { redirect_to root_path }
            end
          end
        end
  
      end
  
    end
    ...
    end
  </code></pre>
  
  <p>Views are in /profiles/edit/*</p>