I would solve this by building a single form with all of the elements, putting it in a partial, and including it on all of the landing pages. Then in the individual stylesheets for each landing page, hide all of the elements that you don't want to use on that particular page.
To solve potential problems with validations (e.g. you need to validate that a password was given when requested) you can create a configuration file (e.g. config/initializers/landing_forms.rb
) where you specify which form fields are to be included in each, then generate the appropriate CSS from that file and also use it in your validations, i.e.:
landing_forms.rb
YourInitializer = {
foo: [:email, :password],
bar: [:email, :name, :address],
baz: [:name, :phone]
}.with_indifferent_access.freeze
_form.html.erb
<%= form_for ... do |f| %>
<%= f.hidden_field_tag :landing_page_id, value: 'foobar' %>
<% end %>
controller
class UsersController < ApplicationController
def create
@user = User.new(params[:user])
User.validate_only requested_fields
# ...
end
protected
def requested_fields
YourConfig.fetch params[:landing_page_id]
end
end
model
class User < ActiveRecord::Base
attr_reader :required_validations
validate :email, presence: true, if: :email_required?
def email_required? # feel free to use a bit of metaprogramming to make this simpler
required_validations.include? :email
end
def validate_only(fields)
@required_validations = fields
end
end
page_forms.css.erb
/* Hide all non-button inputs by default, then show individually */
<% YourConfig.each_pair do |landing_page_id, fields| %>
<% fields.each do |field| %>
#<%= landing_page_id %> form .<% field %> { display: block }
<% end %>
<% end %>
Hopefully that's enough to get you started!