码迷,mamicode.com
首页 > 其他好文 > 详细

Rails accepts_nested_attributes_for表单嵌套的利器

时间:2014-06-22 23:32:23      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   ext   

 

http://blog.csdn.net/kunshan_shenbin/article/details/7249713
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for


 

还在为表单嵌套而苦恼么,看看accepts_nested_attributes_for方法吧,今天在项目里用过之后,更爱这门语言了.

# eoncoding: UTF-8

class Package
  include Mongoid::Document
  include Mongoid::Timestamps
  include ImageUploadable
  store_in collection: all_apps
  field :language, type: String, default: 中文
  field :url, as: :download_url, type: String //as别名
  field :intro_icons, type: Array, default: []
  field :updated_on, type: Date
  field :is_actived, type: Boolean, default: false
  field :position, type: Integer, default: 0
  field :is_deleted, type: Boolean, default: false

  index code: 1 //索引
  index package: 1

  has_many :recommend_apps, foreign_key: app_id
  accepts_nested_attributes_for :recommend_apps

 

class RecommendApp
  include Mongoid::Document
  include Mongoid::Timestamps
  include ImageUploadable

  validates :app_id, presence: true
  validates :type, inclusion: { in: TYPES.keys  }
  validates :app_id, uniqueness: { scope: :type }

  belongs_to :package, foreign_key: app_id

 

<%= form_for @recommend_app, html: { class: package_form_class(@recommend_app), role: form }  do |f|  %>  

  <div class="form-group">
    <%= f.label :position, "推荐位置", class: "col-sm-2 control-label" %>
    <div class="col-sm-3">
      <%= f.text_field :position,:class =>"form-control" %>
    </div>
  </div>

  <%= fields_for :package do |package| %>

  <div class="form-group">
    <%= package.label :title, "名称", class: "col-sm-2 control-label" %>
    <div class="col-sm-3">
      <%= package.text_field :title ,:class =>"form-control" %>
    </div>
  </div>
 <% end %>

recommend_apps_controller.rb

  def edit
    @package = @recommend_app.package
  end 

  def update
    old_position = @recommend_app.position
    if @recommend_app.update_attributes recommend_app_params
      @package = @recommend_app.package
      if @package.update_attributes package_params
        @recommend_app.adjust_recommend_app_position(@recommend_app.position)
        redirect_to recommend_apps_path, notice: 操作成功
      end 
    else
      render :edit
    end 
  end 

 

fields_for(record_name, record_object = nil, fields_options = {}, &block)

可以看下下面的文档

http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-fields_for

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rails accepts_nested_attributes_for表单嵌套的利器,布布扣,bubuko.com

Rails accepts_nested_attributes_for表单嵌套的利器

标签:style   class   blog   code   http   ext   

原文地址:http://www.cnblogs.com/iwangzheng/p/3800124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!