Rails 内置方法大全(慢慢积累中)

1. send_file

send_file => 下载文件
	1. 调用
	send_file(file_path, filename) => file_path(文件路径) file_name(自定义文件name)
	
	2.案列
	# Rails.root(当前项目路径) join(拼接url)
	send_file(Rails.root.join("excel_template/import_hacks_template.xlsx"), filename: "OJ试题导入模版.xlsx")
	
	3.部分源码
	def send_file(path, options = {})
      ActiveSupport::Notifications.instrument("send_file.action_controller",
        options.merge(path: path)) do
        super
      end
    end

2. helper_method

helper_method => 将controller中方法提供给对应模型的view中使用
	1. 案列
	class Demo < ActionController::Base
		helper_method: test
		
		private
		def test
			p "测试 通过helper_method方法是否能将当前控制器下方法在对应模型的view中调用"
		end
	end

	views/demo/index.json.jbuilder
	json.test test 
	
	2. 跟多文档介绍
	https://cloud.tencent.com/developer/ask/sof/90724
	https://www.cnblogs.com/sqycyl/archive/2012/08/16/2641721.html
	https://www.codenong.com/3992659/

3. CGI::escape( str)

CGI::escape( str) => 使用 URL 编码来转义字符串
	1. 案列
	接口有模糊搜索功能很常见 在某接口中携带demo=c++
	最终params[:demo] = 'c  '
	增加 CGI::escape(params[:demo]) = 'c++'

	2. 跟多文档介绍
	https://www.runoob.com/ruby/ruby-cgi-methods.html
	wx收藏照片
	

总结

	1.记录所有rails内置方法简单描述与使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值