Creating a login form using TowerJS

I'll start by saying that I am chiefly a front-end designer/developer and have little experience with MVCs.

I would like to create a login form using TowerJS. Most of my app is out-of-the-box from the docs; I have a single model called "User":

class App.User extends Tower.Model
  @field "email", type: "String"
  @field "userName", type: "String"
  @field "password", type: "String"
  @field "active", type: "Boolean", default: -> true

  @validates "userName", presence: true
  @validates "email", presence: true, format: /\w+@\w+.com/
  @validates "password", presence: true

  @timestamps()

For the "new" user view I include the "form" partial:

formFor @user, (f) ->
  f.fieldset (fields) ->
    fields.field "email", as: "string"
    fields.field "userName", as: "string"
    fields.field "password", as: "string"
    fields.field "active", as: "string"
  f.fieldset (fields) ->
    fields.submit "Submit"

If I use the "form" partial for my login form, it would include fields that I don't want (e.g. "active"). If I use a new partial called "login":

formFor @user, (f) ->
  f.fieldset (fields) ->
    fields.field "email", as: "string"
    fields.field "password", as: "string"
  f.fieldset (fields) ->
    fields.submit "Submit"

then the form doesn't render because it seems like Tower was expecting all of the User fields in the form since it is passed @user. This truly comes down to my lack of understanding on a high level regarding how models are rendered. The two options in my mind for rendering this login form are:

  1. Use the "form" partial in its entirety and hide/ignore the irrelevant fields via css/back-end-js respectively or...
  2. Create some kind of new model that contains only the fields I care about for login (email, password) and then use formFor on that model instead. This seems silly to me since I'm handling users, so I feel intuitively like I should use the User model.

Perhaps the answer lies in creating a Session model, but I'm not sure how all of these parts work together in TowerJS. To be clear, the question is: What is the proper way to render a login form using TowerJS?

Try this:

formFor @user, (f) ->
  f.fieldset (fields) ->
    fields.field 'email', as: 'string'
    fields.field 'userName', as: 'hidden'
    fields.field 'password', as: 'string'
    fields.field 'active', as: 'hidden'
  f.fieldset (fields) ->
    fields.submit 'Submit'

Also, according to the style guide in the docs, you should use single quotes whenever possible.

  1. Make a new file in app/views/users called _login.coffee with only the fields you want. Like this:

    formFor @user, (f) ->
      f.fieldset (fields) ->
        fields.field "email", as: "string"
        fields.field "password", as: "string"
      f.fieldset (fields) ->
        fields.submit "Submit"
    
  2. Edit the file app/views/users/new.coffee so that the line which did read partial "form" now reads partial "login".

Now your view should correctly render. Of course, your now storing a password as plaintext, etc, but that's a whole other story...