Creating a dynamic form and model based on user input

I hope someone can help me do this best. I am setting up a registration form so that people can list their skills / credentials in different jobs.

The circuit goes along these lines using Mongoid and Inheritance.

class Person 
  include Mongoid::Document
  field :name, :type => String 
  field :education, :type => String 
end

class Accountant < Person
  field :cpa, :type => Boolean
  field :active_cpa, :type => Boolean  
end

class SoftwareDeveloper < Person
  field :full_stack, :type => Boolean
  field :language, :type => Array  
end

Thus, users will come to the registration page of mysite.com/persons/new, where there will be a form for selecting various tasks (listed and grouped by various industries: finance, engineering, marketing) and based on the user's choice, a more detailed view will be displayed specifically for this function works. (for example, software developers can list programming languages, and accountants can indicate whether they have a CPA).

: ? 100 , , mysite.com/accountants/new mysite.com/engineers/new / .

-, , ? , jQuery , , ajax , , form_for , , , hash

+3
1

. , form_for Person . fieldset , , :

<%= form_for @person do |f| %>

<fieldset id="base">
  <%= f.label :name %>
  <%= f.check_box :name %>
  <!-- ... -->
</fieldset>

<fieldset id="accountant">
  <%= fields_for :accountant do |f| %>
     <%= f.label :cpa %>
     <%= f.check_box :cpa %>
     <!-- ... -->
  <% end %>
</fieldset>

<fieldset id="software_developer">
  <%= fields_for :software_developer do |f| %>
     <%= f.label :full_stack %>
     <%= f.check_box :full_stack %>
     <!-- ... -->
  <% end %>
</fieldset>

_for , POST "create" , :

params[:person] = { :name => "Joe Smith", ... }
params[:accountant] = { :cpa => true, ... }
params[:software_developer] = { :full_stack => false, ... }

. select:

<%= select_tag :job_type, options_for_select(@job_types) %>

[: job_type] . "create" , , . case:

case params[:job_type]
when :accountant
  @person = Accountant.new(params[:person].merge(params[:accountant]))
when :software_developer
  @person = SoftwareDeveloper.new(params[:person].merge(params[:software_developer]))
when ...
end

, . , , , params [: job_type] , . - :

@person = params[:job_type].constantize.new(params[:person].merge(params[params[:job_type].underscore])

, .

, jQuery, , . .change() select_tag , "id", . , .

, , , .

, , / .

+2

All Articles