Ruby on rails 轨道。从控制器写入/更新第二个模型
我有两种模型:客户端和Vlan。我需要在“f.通过创建“Client”从视图中选择Vlan时将其标记为“active”。 下面是一些来自模型的代码:Ruby on rails 轨道。从控制器写入/更新第二个模型,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我有两种模型:客户端和Vlan。我需要在“f.通过创建“Client”从视图中选择Vlan时将其标记为“active”。 下面是一些来自模型的代码: class Client < ActiveRecord::Base has_many :vlans end class IP < ActiveRecord::Base belongs_to :client end 通过创建“客户机”,我成功地选择了vlan,它在DB中更新(作为vlan_id),但我必须查看哪个客户机绑定到v
class Client < ActiveRecord::Base
has_many :vlans
end
class IP < ActiveRecord::Base
belongs_to :client
end
通过创建“客户机”,我成功地选择了vlan,它在DB中更新(作为vlan_id),但我必须查看哪个客户机绑定到vlan,所以我必须在vlan模型中写入/更新客户机_id
怎么做?我认为我的迁移/关联方式是错误的,但可能控制器也是错误的
class AddClientIdToIPs < ActiveRecord::Migration
def change
add_belongs_to :vlans, :client
end
end
def create
@client = Client.new(client_params)
respond_to do |format|
if @client.save
format.html { redirect_to clients_path, notice: 'Success.' }
#format.json { render action: 'show', status: :created, location: @client }
else
format.html { render action: 'new' }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end