ruby on rails - Creating form for STI Model -


referring post: rails: deal several similar model classes?

using combination of sti , store feature organize similar models.

"declare base model student text column called settings.

class student < activerecord::base   store :settings   # name, email, phone, address etc.. end  class highschoolstudent < student   # declare highschoolstudent specific attributes    store_accessor :settings, :gpa end 

how can create form highschoolstudent, while remaining under student controller?

i dont want add separate controller or route resource highschoolstudent, there way have 1 form student , highschoolstudent, checkbox indicate whether student or highschoolstudent? can require attributes created subclass required form submitted if specific class checked?

<%= simple_form_for(@student, html: {class: "form-horizontal"}) |f| %> 
<%= f.input :name, as: :text, input_html: {rows: "1"} %> <%= f.input :email, as: :text, input_html: {rows: "2"} %>  <%= f.input :gpa, as: :text, input_html: {rows: "1"} %>   <%= f.button :submit, class: "btn btn-primary" %> 

sure, can make arbitrary checkbox, , check value in create action:

# in view <%= check_box_tag :high_school %>  # in controller def create   base_class = params[:high_school] ? highschoolstudent : student   base_class.create(params[:student])   ... end 

any validations specific highschoolstudent ensured if checkbox checked.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -