rails
江城守望者
这个作者很懒,什么都没留下…
展开
-
rails中使用邮件激活账户
生成邮件程序模版rails generate mailer UserMailer account_activation password_reset开发环境中的邮件配置config.action_mailer.raise_delivery_errors = trueconfig.action_mailer.delivery_method = :testhost = 'localhost:3原创 2016-08-11 16:32:21 · 980 阅读 · 0 评论 -
rails中使用markdown编辑
在应用中使用markdown编辑其实很简单,只需要一个gemredcarpet。假如我们有一个文章article的功能,文章的编辑需要使用markdown,我们可以这样做: 在articles 中添加两个字段,content 和content_html,前者存储文章内容,后者存储markdown格式的文章内容(显示的时候直接使用simple_format输出,不需要再次解析成markd原创 2018-01-09 16:30:45 · 466 阅读 · 0 评论 -
rails程序中加载js
常用的方式有两种: 1. 直接在view中写javascript代码; 2. 通过Asset Pipeline加载app/assets/javascripts中的js文件对于第一种方式,我的习惯做法是在app/views/layout/application.html.erb中定义名称为javascript的yield,然后在需要js的view中使用content_for插入js代码:原创 2018-01-05 14:49:59 · 2156 阅读 · 0 评论 -
rails返回上一次访问的页面
最近在做个人网站的时候遇到这个问题,之前习惯使用redirect_to :back,当前使用rails 5.1.4,抛出错误undefined method 'back_url',原来rails5.1需要使用redirect_back :redirect_back(fallback_location: admin_user_images_path)原创 2017-12-29 21:57:21 · 1154 阅读 · 0 评论 -
rails5.1 belongs_to关联数据必须存在
# app/models/order.rbclass Order < ApplicationRecord belongs_to :paymentend# app/models/payment.rbclass Payment < ApplicationRecord has_many :ordersend业务场景中,先有订单order,然后才生成的支付数据payment,但是订单保存的时原创 2018-01-03 17:28:42 · 1112 阅读 · 0 评论 -
Ubuntu 16.04上使用Nginx Passenger 部署 Rails
本文参照Ruby China的教程,在阿里云ubuntu 16.04上部署自己的rails应用,逐步列出遇到的问题及其解决方案创建账号以root账户登录服务器,运行: $ useradd -m -s /bin/bash deploy$ adduser deploy sudo$ passwd deploy退出root账户,以deploy登录。使用 RVM 安装 Ruby原创 2018-01-11 20:33:28 · 1283 阅读 · 0 评论 -
rails test 加载seeds.rb的两种方法
执行rails test时,rails默认执行下面的操作:Remove any existing data from the table corresponding to the fixtureLoad the fixture data into the tableDump the fixture data into a variable in case you want to access原创 2017-11-07 12:33:27 · 975 阅读 · 0 评论 -
I18n实现中英文切换
Rails I18n API框架提供了 Rails 应用国际化/本地化所需的全部必要支持,可以很容易的实现网站语言的切换。配置I18n模块Rails 会把 config/locales 文件夹中的 .rb 和 .yml 文件自动添加到翻译文件加载路径中,默认的翻译文件是config/locales/en.yml,假如我们需要使用自定义的翻译文件config/locales/zh.yml作为默认翻译,原创 2017-12-12 16:10:30 · 4977 阅读 · 0 评论 -
MiniTest学习笔记
当前rails版本为5.1.4,本身自带MiniTest. 当我们执行rails g model customer时,会自动生成test/models/customer_test.rb和test/fixtures/customers.ymlrequire 'test_helper'class CustomerTest < ActiveSupport::TestCaseendActiveSupp原创 2017-11-01 11:35:24 · 1192 阅读 · 0 评论 -
为所有model添加通用的方法
业务代码中,我们经常会用到各种状态、类型表,通过判断状态或类型来设计业务逻辑。比如说我们有订单orders和订单状态order_statuses,当订单为草稿状态时可以继续往下操作,常见的判断:if order.order_status_id == 1 do_sthend为避免产生魔法发数字,我们会这么写:status_id = OrderStatus.find_by_name("草稿").t原创 2017-11-08 12:12:49 · 840 阅读 · 0 评论 -
mac安装therubyracer出错
执行bundle install后报错如下:An error occurred while installing libv8 (3.16.14.17), and Bundler cannot continue. Make sure that `gem install libv8 -v '3.16.14.17'` succeeds before bundling.于是,执行gem install l原创 2017-01-09 16:49:25 · 983 阅读 · 0 评论 -
阿里云部署rails应用遇到的问题与解决方案
git clone失败:Permission denied (publickey) 原因:缺少ssh keys 解决方法:执行ssh-keygen -t rsa生成public key,拷贝上述命令生成的id_rsa.pub文件中的值,将其添加到github的ssh keys中。原创 2016-12-22 21:59:27 · 859 阅读 · 0 评论 -
Rails 5中使用Bootstrap
首先在Gemfile文件中添加:gem 'bootstrap-sass', '3.3.6'然后执行bundle install接下来,在css文件中加入两行代码:@import "bootstrap-sprockets";@import "bootstrap";可能出现的问题Invalid CSS after "body ": expected selector or at-rule, wa原创 2016-12-08 11:53:36 · 2483 阅读 · 0 评论 -
devise实现用户登录
gem安装Gemfile 文件中添加 gem 'devise',执行bundle installrun the generatorrails g devise:install 执行之后需要完成以下四步。 在config/environments/development.rb中添加 config.action_mailer.default_url_options = { host: 'local原创 2016-12-06 17:12:36 · 450 阅读 · 0 评论 -
日常开发技巧(二)
Terminal光标移动经常写一些比较长的查询,有时会有几行,然后发现开头的地方有错误,这个时候可能要按住箭头,一个一个字符地把光标移到开头去,其实可以用快捷键去实现。跳到行首:Ctrl + A 跳到行尾:Ctrl + E 删除光标右边的所有内容:Ctrl + K 删除光标左边的内容(一次一个单词):Ctrl + W用_访问最后一个表达式经常会做这样的事:在console中查询一个对象,忘了把原创 2016-12-05 10:46:23 · 286 阅读 · 0 评论 -
日常开发技巧(一)
rails console使用技巧console是我喜欢上rails的原因之一,可以加载rails环境,可以快速的进行测试和调试。常见用法Rails Console 窍门清空 Console Mac Command + Kboth Mac and Linux Ctrl + L重新加载 reload原创 2016-08-10 17:50:23 · 358 阅读 · 0 评论 -
rails实现PDF文档导出功能
业务系统中,经常有导出PDF的需求,现使用prawn来实现这一功能。gem install prawn;bundle exec rake manual生成manual.pdf帮助文档;将自己需要的字体拷贝至"#{Prawn::DATADIR}/fonts/"路径下;目前的需求很简单,只需要将文字内容转化成pdf:def generate_pdf(content) P原创 2018-01-10 20:20:19 · 738 阅读 · 0 评论