在保存操作中使用父值的 Rails3 嵌套属性 [英] Rails3 Nested Attributes Using Parent Values in Save Action

查看:35
本文介绍了在保存操作中使用父值的 Rails3 嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rails3 应用程序中有一个简单的嵌套表单.我正在尝试研究如何在保存时将父模型中的值保存到子模型中.

I've got a simple nested form working in my rails3 application. I'm trying to work out how to save a value from the parent model into the child when saving.

class Radcheck < ActiveRecord::Base
  set_table_name 'radcheck'
  attr_accessible :attribute_name, :username, :value, :op
  belongs_to :raduser  
end

class Raduser < ActiveRecord::Base
  has_many :radcheck, :dependent => :destroy
  accepts_nested_attributes_for :radcheck  
end

还有我的表格:

<%= form_for @raduser do |f| %>  
  <p>  
    <%= f.label :username %><br />  
    <%= f.text_field :username %>  
  </p>  
  <%= f.fields_for :radcheck do |builder| %>  
  <li>  
    <%= builder.label :attribute_name %><%= builder.text_field :attribute_name %>  

  </li>  
  <% end %>  
  <p><%= f.submit "Submit" %></p>  
<% end %>

我想要做的是在保存时将 Raduser.username 值保存到 radcheck 表中.对于每条记录.

What I want to do is save the Raduser.username value in to the radcheck table on save. For each record.

我试过在控制器中放入一些东西,但这对我们来说并没有真正起作用.

I've tried putting something in the controller but that wasn't really working for us.

-- 更新--

在我的 radcheck 控制器中,我已经尝试过这个(作为测试),但值没有保存.

In my radcheck controller, I've tried this (as a test) but the values don't save.

def create
      @radcheck = Radcheck.new(params[:radcheck])
      @radcheck.username = '123'
      respond_to do |format|
        if @radcheck.save

          format.html { redirect_to @radcheck, notice: 'Radcheck was successfully created.' }
          format.json { render json: @radcheck, status: :created, radcheck: @radcheck }
        else
          format.html { render action: "new" }
          format.json { render json: @radcheck.errors, status: :unprocessable_entity }
        end
      end
    end

也曾在 radusers 中尝试过,但出现错误.

Have also tried in radusers but that gave error.

推荐答案

看起来您发布的控制器代码是针对 Radcheck 控制器的,对吗?如果是这样,您发布的表单将使用 RaduserController 类的 create 操作,而不是来自 RadcheckController 的操作.这将解释为什么您没有在 radcheck 行中看到用户名123".

It looks like the controller code you posted is for the Radcheck controller, correct? If so, the form you have also posted will be using the create action of the RaduserController class, not the one from RadcheckController. This would explain why you aren't seeing the username '123' in the radcheck rows.

如果父级和子级之间的用户名字段相同,同步这两者的常用方法是使用观察者或 before_save 回调.我将在下面概述 before_save 方法:

If the username field is the same between parent and child, a common way to sync these two up would be with an observer or a before_save callback. I'll outline the before_save method below:

class Radcheck < ActiveRecord::Base
  set_table_name 'radcheck'
  attr_accessible :attribute_name, :username, :value, :op
  belongs_to :raduser

  before_save :sync_usernames

private

  def sync_usernames
    self.username = self.raduser.username
  end
end

我已经用 Rails 3.1 测试过它,它可以工作;但是,我知道在以前版本的 Rails 中访问子模型中的父模型时遇到了问题.在这些情况下,我必须将 before_save 操作放在父模型上,并让它遍历每个子模型,以这种方式设置适当的属性.

I've tested this with Rails 3.1 and it works; however, I know I have run into issues accessing parent models within a child model in previous versions of Rails. In those cases, I had to put the before_save action on the parent model, and have it iterate over each child, setting the appropriate attributes that way.

这篇关于在保存操作中使用父值的 Rails3 嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆