繁体   English   中英

一视图两个控制器

[英]one view two controllers

我的应用有安装控制器和地址控制器。

Address has_one :installation and Installation belongs_to :address

在安装视图中,我在另一个simple_form中获得了simple_form。 像这样:

<%= 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'
                %>

那么如何更新两个模型?

我可以有两个def参数吗? 喜欢这个:

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

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

您可以使用嵌套属性

未经测试,但大致:

模型:

class Installation < ActiveRecord::Base
  belongs_to :address

  accepts_nested_attributes_for :address
end

在您的InstallationsController

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM