views:

498

answers:

3

I'd like to create a user registration form where the user ticks some boxes that do not connect to the model.

For example, there might be a 'terms & conditions' box that I don't want to have a boolean field in the User model saying 'ticked Terms & Conditions'. Instead I want to create a record somewhere else (like a transaction) that recorded the date/time they accepted the T&Cs.

Another example might be some preference they indicated that I'll use later and hold in the session for now, like 'remember me'.

I can mix these types of fields with the regular form helper. How could I do either one of the examples above when using formtastic? It kind of sticks to have to mix traditional rails tags with lovely clean formtastic code.

+1  A: 

You can create any number of virtual attributes in your model that do not necessarily need to be tied to a database column. Adding attr_accessor :terms_and_conditions to your user model will make this 'field' available to formtastic -- even though it's not a database field. You can validate it like any other field or create your own setter method to create a record elsewhere if that's what you need.

bensie
A: 

Instead I want to create a record somewhere else (like a transaction) that recorded the date/time they accepted the T&Cs.

Use the attr_accessor that bensie describes to integrate the field with formtastic.

Formtastic is about view logic, while the relationship are more model logic. Don't worry about creating the related record in the form. Instead, use callbacks like before_update and after_save in the model to ensure the related record has been created.

EmFi
+1  A: 

I'm inclined to disagree with the approach to use attr_accessors for action-specific entry elements. If Ts&Cs need to be recorded then that makes sense, but sometimes you need data that really is unrelated to the model and is only related to the specific action at hand, such as 'perform some heavyweight operation when executing the action'.

Lets say you have a sign-up form, and you're not using OAuth, and you have an option to specify twitter username and password on sign up. This is fine:

<%= form.input :twitter_username %>
<%= form.input :twitter_password, :as => :password %>

But this bit below confuses me -- its like formtastic in this case is actually taking away what is already there. Is there a way of adding params[:your-object] while still getting formastic to do all its lovely layout stuff?

How about:

class User < ActiveRecord::Base
...
  #I don't want this here. Its only for UserController#create.
  #attr_accessor :tweet_when_signed_up 
...
end

and:

<%= form.input :tweet_when_signed_up, :as => :checkbox, :param_only => true %>

param_only is my made-up suggestion. It says 'this isn't even a transient property. Its just for this action.

class UserController < ActionController::Base
...

def create
  if params[:tweet_when_signed_up] # haven't done this yet -- == 1 or !.nil?
     Tweeter.tweet( ... )
  end
  @user = User.create( params[:user] )
end

The ability to do this is probably there -- does anyone know how to do effectively what I think is a good idea above?

thanks!

Julian