1. send_file
send_file => 下载文件
1. 调用
send_file(file_path, filename) => file_path(文件路径) file_name(自定义文件name)
2.案列
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内置方法简单描述与使用