rails: a single simple_form with two unrelated models?

喜你入骨 提交于 2019-12-06 15:16:03

问题


I want to use simple_form_for to display a form that contains inputs for creating an instance of model A and separately an instance of model B. I want to have a single submit button for the form. Models A and B are not associated in any way, so simple_fields_for doesn't seem to apply here. Ideally, params would contain two hashes, under the keys A and B, so that each object's fields are grouped together. Is there a way to achieve this using simple_form? Thanks.


回答1:


You could use fields_for helper:

    = form_tag action_path do

      = fields_for :model_a do |a|
        = a.text_field :name

      = fields_for :model_b do |b|
        = b.text_field :name

      = submit_tag 'Submit'


来源:https://stackoverflow.com/questions/23622709/rails-a-single-simple-form-with-two-unrelated-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!