王筝的博客
ruby学习
- validates :name, presence: true, uniqueness: { conditions: -> { where(:state.ne => 2) } }, length: { maximum: 9 }
+ validates :name, presence: true, length: { maximum: 9 }

validates :name, presence: true, uniqueness: { conditions: -> { where(:state.ne => 2) }, scope: :version}, length: { maximum: 9 }

 

<td><%= number_to_currency product.price, unit: "¥" %></td>

 

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
field :intro_icons, type: Array, default: []
field :updated_on, type: Date
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