zoukankan      html  css  js  c++  java
  • rails中accepts_nested_attributes_for应用

    Model:

    class Blog < ActiveRecord::Base
      has_many  :strip_rules
      accepts_nested_attributes_for :strip_rules, allow_destroy: true
    end
    

      

    class StripRule < ActiveRecord::Base
      belongs_to :blog
      attr_accessible :rule, :blog_id
    end
    

      

    要实现在新建和修改blog时可以添加/删除任意多个strip_rule

     我们用rails中得 accepts_nested_attributes_for实现这个功能。

    新建和修改页面调用模板页面_form.html.erb

    #form页面主要实现代码
    <%= f.fields_for :strip_rules do |strip_rule| %>
        <%= render "strip_rule", :f => strip_rule %>
    <% end %>
    <p><%= raw link_to_add_fields("增加一条新规则", f, :strip_rules) %></p>
    <div class="form-group">
        <%= f.submit '确定', class: 'btn btn-default' %>
    </div>
    

    代码中调用子模板页 _strip_rule.html.erb

    <p class="fields">
      <%= f.label :rule, "内容" %>
      <%= f.text_field :rule %>
      <%= link_to_remove_fields "remove", f %><br />
    </p>
    

    form页面用到得helper方法

    module BlogAdminHelper
      def link_to_remove_fields(name, f)
        f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
      end
      
      def link_to_add_fields(name, f, association)
        new_object = f.object.class.reflect_on_association(association).klass.new
        fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
          render(association.to_s.singularize, :f => builder)
        end
        link_to_function(name, raw("add_fields(this, "#{association}", "#{escape_javascript(fields)}")"))
      end
    end
    

    以及form页中需要用到得js

    <script type="text/javascript">
      function remove_fields(link) {
        $(link).prev("input[type=hidden]").val("1");
        $(link).closest(".fields").hide();
      }
    
      function add_fields(link, association, rule) {
        var new_id = new Date().getTime();
        var regexp = new RegExp("new_" + association, "g");
        $(link).parent().before(rule.replace(regexp, new_id));
      }
    </script>
    

    其中遇到最麻烦的问题是增加一条新内容时html解析出很多&quot; ,导致调用js报错。

    解决办法:使用rails提供的辅助方法raw,会调用html_safe方法,去掉&quot;

    raw("add_fields(this, "#{association}", "#{escape_javascript(fields)}")")
    

    最终效果

  • 相关阅读:
    系统编码、文件编码与python系统编码
    python2判断编码格式
    android: 对apk进行系统签名
    android: 对普通的apk应用进行签名
    android studio: 设置单条注释不换行
    Gradle: Could not determine the dependencies of task ':app:processDebugResources'解决(转)
    android Dialog: 去除dialog 顶部 蓝色的线
    android:Error:” ” is not translated in “en” (English) [MissingTranslation]处理方法(转)
    android: 通过Intent筛选多种类型文件
    GIt: git rebase 和 git merge 的区别 (转)
  • 原文地址:https://www.cnblogs.com/wangyuyu/p/4087815.html
Copyright © 2011-2022 走看看