1:helper_method :current_user #这样可以在页面上调用current_user方法 2:include UserHelper 这样可以在controller中调用helper内的方法 3:Rails.application.routes.url_helpers.urlhelper方法名 这样在model中可以调用routes中定义的路由 或者使用include Rails.application.routes.url_helpers 源码位置: actionpack/lib/action_dispatch/routing/url_for.rb # class User < ActiveRecord::Base # include Rails.application.routes.url_helpers # # def base_uri # user_path(self) # end # end # # User.find(1).base_uri # => "/users/1" # 或者直接使用Rails.application.routes.url_helpers.user_path(self) 4:after_save和after_commit after_save是在数据库中添加数据完成后执行的 after_commit 是rails把每次和数据库交互当成一个事务,begin 。。save。。 commit 之后执行 after_commit do |good| puts good.name end after_save do |good| puts good.name + "三三三四四" end 输出: 2.2.1 :067 > Good.build_for_demo({}).save (0.4ms) BEGIN SQL (0.5ms) INSERT INTO `goods` (`name`, `spec`, `ref_price`, `points`, `par`, `created_at`, `updated_at`) VALUES ('示例奖品', '250ml', 6.0, 120, 100, '2018-02-05 09:33:31', '2018-02-05 09:33:31') 示例奖品三三三四四 (2.1ms) COMMIT 示例奖品 => true 5:routes中 namespace 和 scope区别 6:with_options unless: :pending? do # with_options unless: :pending? do validates :name, presence: true validates :code, presence: true validates :code, uniqueness: true end # 帐号rejected状态,需要验证rejected_desc validates :rejected_desc, presence: true, if: :rejected? # 帐号enabled expired状态,需要验证expires_at validates :expires_at, presence: true, if: "enabled? || state.expired?"
http://blog.csdn.net/huopo125/article/details/384671557: accepts_nested_attributes_for: class Blog < ActiveRecord::Base has_many :posts has_one :author accepts_nested_attributes_for :author, :reject_if => :all_blank accepts_nested_attributes_for :posts, :reject_if => :any_blank end 8: Admin::GoodsController .new.send(:_prefixes) => ["admin/goods", "admin/base", "application"] How Rails Finds Your Templates 9: 为什么子类都要实现self.model_name @good 页面上使用的是统一的[:admin,@good], 这样@good 的model_name 才会访问路由/admin/goods/ceate 10 : config.wrappers :horizontal_input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 1 :b.use :html5 2 :b.optional :readonly 3 :b.use :label, class: 'col-md-3 col-lg-2 control-label' 4 :b.wrapper tag: 'div', class: 'col-md-4 col-lg-3' do |wr| 5 :wr.wrapper tag: 'div', class: 'input-group' do |ba| ba.use :input, class: 'form-control' end 6 :wr.use :error, wrap_with: { tag: 'span', class: 'help-block' } 7 :wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' } end end simple_form gem 中,代码位置config/wrap_parameters.rbzhong 第一行:使用html5 、 第二、三行翻译为: <label class="col-md-3 col-lg-2 control-label"> 第四 ---7 行翻译为: <div class="col-md-4 col-lg-3"> 第5 行:<div class="input-group"> <input class="form-control"> </div> 第7 行:<p class="help-block"></p> </div> 使用方法: <%= f.input :points_par, wrapper: :horizontal_input_group do %> <%= f.input_field :points_par, as: :integer, value: f.object.points_par.to_i, class: "form-control", step: 1 %> <span class="input-group-addon"> 积分</span> <% end %> 11 : simple_form 下拉狂内容定制显示label_method: <%= f.input :par, label: " 话费额", collection: [1,10,20,30,50,100], label_method: ->(n){ "#{n} 元"}, include_blank: false, hint: " 话费为全国全网通用" %> 12 : lookup_context.exists?(@good.type_name, "admin/goods/form", true) views 路径下是否存在某个文件 13: 为了保证一个子事务的 rollback 被父事务知晓,必须手动在子事务中添加 :require_new => true 选项。比如下面的写法: 14: 重定向路由并且传递常数,"filter"=>{"state"=>:submitted} resources :approvals do collection do get :approved, to: "approvals#index", defaults: { filter: { state: :approved }} get :submitted, to: "approvals#index", defaults: { filter: { state: :submitted }} end end
15: ActiveRecord::Rollback 不会传播到上层的方法中去,为了保证一个子事务的 rollback 被父事务知晓,
必须手动在子事务中添加 :require_new => true
User.transaction do User.create(:username => 'Kotori') User.transaction(:requires_new => true) do User.create(:username => 'Nemu') raise ActiveRecord::Rollback
end
end