Topic: Refactor Help Needed

I need some help with a quick refactoring I decided to do.

I have a users controller and because of the fields and validations on those fields I wanted to pull the register portion out of the controller and put a new/create in its own controller called admits.

however upon doing so my view of Controllers was quickly brought to the forefront. I think my knowledge is lacking.

I created the Controller with script/generate and it is called Admits

In routes declared
...
  map.resources :admits

  map.resources :users
...

Admits has two actions new and create... the others are blank.

class AdmitsController < ApplicationController

Controller Admits
# GET /admits/new
# GET /admits/new.xml
def new
    @admit = User.new
    @state = State.all(:order => :abbreviation)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @admit }
    end
  end

  # POST /admits
  # POST /admits.xml
  def create
    @admit = User.new(params[:admit])
    @state = State.find(:all, :order => :abbreviation)

    respond_to do |format|
      if @admits.save
        flash[:notice] = "User #{@admit.username} was successfully created."
        format.html { redirect_to blogs_url }
        format.xml  { render :xml => @admit, :status => :created, :location => @admit }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @admit.errors, :status => :unprocessable_entity }
      end
    end
  end

  def index
  end

  def edit
  end

  def update
  end
end

The problem I am seeing is that the new action is submitting the data through the User.create action and there is some code in the User view causing the create to choke. I fear this is a shortfall in my understanding of RESTful routing.

What is the proper way to get new to POST via admits create action?
Do I need to make Admits a subclass of UserController instead of ApplicationController?
Is it a problem with routes?

Any help or pointing in the right direction is appreciated. If you need more code let me know but there really isn't much more that is specific to Admits.

Also in the view there is a new.html.erb file

Last edited by engineerDave (2009-09-04 22:27:24)

"If I'd asked my customers what they wanted, they'd have said a faster horse." - Henry Ford

Re: Refactor Help Needed

The form code is pretty important if you want to know why it won't post to your create action.

Re: Refactor Help Needed

Sorry for the confusion. I was just getting ready to follow up this post with a link to this article with a nice picture that clearly shows my understanding of the Controller interaction was flawed.

http://darynholmes.wordpress.com/2008/0 … rt-1-of-n/

So much to learn so little time. Thanks for the assist though...

"If I'd asked my customers what they wanted, they'd have said a faster horse." - Henry Ford