Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用RubyonRails初始化嵌套的输入字段?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何使用RubyonRails初始化嵌套的输入字段?

Ruby on rails 如何使用RubyonRails初始化嵌套的输入字段?,ruby-on-rails,Ruby On Rails,我正在创建一个以代码为业务键的值(id、代码、名称、父id)的分层列表。这意味着,当用户希望将值附加到父项时,他会填写父项代码字段,在保存之前,模型会关联父项值id。value.rb模型有一个虚拟属性:父项代码,以及以下在保存之前的操作: class Value < ActiveRecord::Base extend CsvHelper # This virtual attribute allows user to ignore parent value id attr_accessor

我正在创建一个以代码为业务键的值(id、代码、名称、父id)的分层列表。这意味着,当用户希望将值附加到父项时,他会填写父项代码字段,在保存之前,模型会关联父项值id。value.rb模型有一个虚拟属性:父项代码,以及以下在保存之前的操作:

class Value < ActiveRecord::Base
extend CsvHelper

# This virtual attribute allows user to ignore parent value id
attr_accessor :parent_code

### scope
#  Value is linked to a list which belongs to the correct scope

### before filter
before_save :set_parent_id # user only inputs parent code

### validation
    validates :code, presence: true
    validates :name, presence: true

    belongs_to :values_list
    has_many :subs, :class_name => "Value", :foreign_key => :parent_id
    belongs_to :superior, :class_name => "Value", :foreign_key => :parent_id

### private functions definitions
  private

    ### before filters
    def set_parent_id
        self.level = 1
        if not self.parent_code.blank?
             @parent = Value.where("code = ? and values_list_id = ?", self.parent_code, self.values_list_id).first
             self.parent_id = @parent.id
             self.level = @parent.level.next
        end
    end
end
类值“Value”、:foreign\u key=>:parent\u id
属于:上级,:类\u名称=>“值”,:外部\u键=>:父\u id
###专用函数定义
私有的
###过滤器前
def set_父项_id
self.level=1
如果不是self.parent\u code.blank?
@parent=Value.where(“code=?和values\u list\u id=?”,self.parent\u code,self.values\u list\u id)。首先
self.parent_id=@parent.id
self.level=@parent.level.next
结束
结束
结束
此模型用作值列表模型的嵌套对象。这在创建时工作良好,并产生预期的结果

但在编辑记录时,父代码属性绝对不会初始化,因此现在显示在输入字段中。以下是嵌套表单:

<table class="table table-striped table-condensed">
  <tr align="left">
    <th></th>
    <% if @values_list.is_hierarchical %>
      <th> <%= t('ParentCode') %> </th>
      <th> <%= t('Level') %> </th>
    <% end %>
    <th> <%= t('Code') %> </th>
    <th> <%= t('Value') %> </th>
    <th> <%= t('Description') %> </th>
  </tr>
  <%=  f.nested_fields_for :values, @values_list.values.order(:code), wrapper_tag: 'tr' do |value| %>
    <td><%= value.remove_nested_fields_link {image_tag("remove.png")} %></td>
    <% if @values_list.is_hierarchical %>
      <td><%= value.text_field :parent_code, ***# code to initialise #*** %> </td>
      <td><%= value.text_field :level, disabled: true %> </td>
    <% end %>
    <td><%= value.text_field :code %> </td>
    <td><%= value.text_field :name %> </td>
    <td><%= value.text_field :description %> </td>
  <% end %>
</table>

如何使用myvalue.superior.code初始化输入字段


非常感谢

您可以访问在的
f.nested_fields_中使用的表单对象,并以这种方式设置值

 <%=  f.nested_fields_for :values, @values_list.values.order(:code), wrapper_tag: 'tr' do |value| %>
   ...
      <td><%= value.text_field :parent_code, value: value.object.superior.code %> </td>
      <td><%= value.text_field :level, disabled: true %> </td>
    ...

...
...
您可能希望查看而不是在翻译文件的根目录中添加一组键,如
ParentCode
。随着应用程序的增长,它会变得非常混乱。