Frage

I'm trying to build a form which preloads content from two models (two variables being passed, being shown in the textfields) and then, not saves the data but sends the altered content (from the textfields) as two(?) variables to a mailer class.

I've managed to preload the data from one of the two models but am not sure how the form_for tag has to look like to get both models loaded as well as targeting the mailer class method instead of updating the model entity when pressing "send".

Do I need the accepts_nested_attributes_for attribute inside the model if I'm not saving anything?

I hope someone could give me an small example of the crucial parts. A thousand thanks!

War es hilfreich?

Lösung

You can use fields_for to include other models in same form. You can use it inside the same form_for what is present.

Checkout the example here from the api docs,

  <%= form_for @person do |person_form| %>
      First name: <%= person_form.text_field :first_name %>
      Last name : <%= person_form.text_field :last_name %>

      <%= fields_for @person.permission do |permission_fields| %>
        Admin?  : <%= permission_fields.check_box :admin %>
      <% end %>

      <%= f.submit %>
    <% end %>

when you submit the data from this form, you can just use that data to pass to the mailer class from controller. UserMailer.get_user_info(params[:name], params[:address]).send

 Creates a scope around a specific model object like #form_for, but doesn't create the form tags themselves. This makes #fields_for suitable for specifying additional model objects in the same form.

Refer Docs here:. fields_for(record_name, record_object = nil, options = {}, &block)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top