Rails 平时的一些小技巧

1helper_method :current_user #这样可以在页面上调用current_user方法
2include UserHelper 这样可以在controller中调用helper内的方法
3Rails.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_saveafter_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
  5routesnamespace 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/38467155
 7: 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值