volt
volt

Reputation: 357

one view two controllers

My app got installation controller and address controller.

Address has_one :installation and Installation belongs_to :address

In my installation view, I got a simple_form inside in other simple_form. Like this:

<%= simple_form_for @installation, class: 'form-horizontal' do |f| %>
    <%= f.error_notification %>

        <%= f.simple_fields_for @installation.address do |u| %>
              <%= u.label :street_address, label: t('address.address_label'), required: true, class: 'col-sm-2 control-label' %>
                <%= u.input_field :street_address, class: 'form-control'
                %>

So how I can update the two models ?

Can I have two def params ? Likes this:

def installation_params
    params.require(:installation).permit(x)
end

def installation_address_params
    params.require(:????).permit(y)
end

Upvotes: 0

Views: 98

Answers (1)

thebenedict
thebenedict

Reputation: 2589

You can use nested attributes.

Untested, but roughly:

Model:

class Installation < ActiveRecord::Base
  belongs_to :address

  accepts_nested_attributes_for :address
end

And in your InstallationsController:

params.require(:installation).permit(...,
  address_attributes: [:id, ...])

Upvotes: 3

Related Questions