Ruby on Rails errors:
(新手期)
Q1: rvm 无法使用
$ rvm use 1.9.3 --default
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.
使用:
source ~/.rvm/scripts/rvm
或者:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >>~/.bashrc
source ~/.bashrc
Q2: bundle install httpError
run bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching source index from https://rubygems.org/
Retrying fetcher due to error (2/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org/
Retrying fetcher due to error (3/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org/
Retrying fetcher due to error (4/4): Bundler::HTTPError Could not fetch specs from https://rubygems.org
[^网络原因,需要翻墙]:
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
gem sources -l
gem install rails #如果装rails时失败,就需要重装
#rails 项目需要使用Gemfile 和 Bundle 我们可以用 Bundler 的 Gem 源代码镜像命令。
bundle config mirror.https://rubygems.org https://gems.ruby-china.org
#这样你不用改你的 Gemfile 的 source
参考链接:ruby-china镜像站 快如闪电!
Q3: 生产秘钥 rails4 rails5 不同
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit`
需要添加生产密钥
在rails4 中这个密钥是通过 rake secret RAILS_ENV=production
创建后放到服务器端的/home/xxx/your_web_app/shared/config/secrets.yml中的,而在rails5中改成了master.key ,使用时需要在本地的/home/xxx/your_web_app/config/deploy.rb 的:link_file手动添加 ‘config/master.key’
如:
# Default value for :linked_files is []
append :linked_files, "config/database.yml", "config/master.key"
Q4: rake aborted!
$ sudo rake secret --trace
rake aborted!
LoadError: cannot load such file -- bundler/setup
这一个可以尝试提权,用root权限运行
Beginning in Rails 4, Rails ships with a
rails
binstub at ./bin/rails that should be used instead of the Bundler-generatedrails
binstub.
Q5: The page you were looking for doesn't exist.
The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.
本地测试可以,远程服务器不行.检查了一下,路由有问题,处理好远程仓库的代码(因为服务器上应该是从远程仓库上拉取代码的)
Q6: 下拉菜单无法下拉 (使用bootstrap)
下拉菜单无法下拉 (使用bootstrap)
这个功能需要js支持,所以需要包含必要的js库,在/app/assert/javascript/application.js里面添加
//= require jquery
Q7:.... ? ..... : .....
syntax error, unexpected ';' : expecting keyword_end ...cost?BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
这是一个.... ? ..... : ..... 的表达式,居然没有发现,因此中间的问号与前面的判断条件需要有一个空格隔开。
Q8: 开放stmp服务的25端口
F, [2018-09-06T20:33:15.955037 #21413] FATAL -- : [bdf765fc-3c26-4d8f-87be-c5357887f443] ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):
F, [2018-09-06T20:33:15.955131 #21413] FATAL -- : [bdf765fc-3c26-4d8f-87be-c5357887f443] 3: <p>
[bdf765fc-3c26-4d8f-87be-c5357887f443] 4: Welcome to the Sample App! Click on the link below to activate your account:
[bdf765fc-3c26-4d8f-87be-c5357887f443] 5: </p>
[bdf765fc-3c26-4d8f-87be-c5357887f443] 6: <%= link_to "Activate", edit_account_activation_url(@user.activation_token,
[bdf765fc-3c26-4d8f-87be-c5357887f443] 7: email: @user.email) %>
F, [2018-09-06T20:33:15.955158 #21413] FATAL -- : [bdf765fc-3c26-4d8f-87be-c5357887f443]
F, [2018-09-06T20:33:15.955180 #21413] FATAL -- : [bdf765fc-3c26-4d8f-87be-c5357887f443] app/views/user_mailer/account_activation.html.erb:6:in `_app_views_user_mailer_account_activation_html_erb__374929091__625207508'
[bdf765fc-3c26-4d8f-87be-c5357887f443] app/mailers/user_mailer.rb:11:in `account_activation'
[bdf765fc-3c26-4d8f-87be-c5357887f443] app/models/user.rb:48:in `send_activation_email'
[bdf765fc-3c26-4d8f-87be-c5357887f443] app/controllers/users_controller.rb:18:in `create'
首先是我看的教程没有配置邮件服务器的主机host
然而配置完成后又出现内网连接超时的问题,
I, [2018-10-07T23:58:53.103195 #26716] INFO -- : [084c0592-31a4-492c-8686-9cac1b6d2ead] Completed 500 Internal Server Error in 30173ms (ActiveRecord: 7.9ms)
F, [2018-10-07T23:58:53.104096 #26716] FATAL -- : [084c0592-31a4-492c-8686-9cac1b6d2ead]
F, [2018-10-07T23:58:53.104154 #26716] FATAL -- : [084c0592-31a4-492c-8686-9cac1b6d2ead] Net::OpenTimeout (execution expired):
F, [2018-10-07T23:58:53.104187 #26716] FATAL -- : [084c0592-31a4-492c-8686-9cac1b6d2ead]
F, [2018-10-07T23:58:53.104220 #26716] FATAL -- : [084c0592-31a4-492c-8686-9cac1b6d2ead] app/models/user.rb:48:in `send_activation_email'
[084c0592-31a4-492c-8686-9cac1b6d2ead] app/controllers/users_controller.rb:18:in `create'
结果发现是:腾讯云封禁了stmp服务的25端口,so。。工单申请解禁之后就可以正确发送激活的邮箱了。
Q7: 服务器端运维
如何在服务器端增删model对象的属性
在current文件夹下运行 rails console prodution才能正常访问生产环境下的数据信息
因为模型的文件model/在当前项目文件夹是没有文件的,即便rails可以运行,他也找不到model对象就会出现:
irb(main):001:0> User.first
Traceback (most recent call last):
1: from (irb):1
NameError (uninitialized constant User)
另外需要注意的是,如果本地用的mysql和远程使用的mysql密码不一样的话,,database.yml的配置也要不一样
服务器端的用户模型已经有reset_at 属性,使用的迁移肯定有问题
要注意旧的时间戳的迁移,在已经执行过迁移之后重新使用db:migrate是没有效果的,除非db:rollback之后才有效果,想要对数据库做新的增删,需要建立新的迁移文档d
本地测试非常非常重要啊小老弟
所以别在瞎提交本地分支到服务器进行部署,弄错了,麻烦得一匹
Q9: 科学计算环境
对了不要别的什么科学计算环境下运行rails虚拟环境
其实就是anaconda,会触发LoadError
Q10: 测试固件
测试失败 之 固件错误 显示nil
解决方法: 在test/fixtures/xxx.yml 中添加所需要的测试固件
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
view的变量是nil ,所以无法渲染界面
解决: 检查对应的controller 中是否定义了这个变量
Q11: Gemfile与本地的包版本不同
/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/lib/bundler/runtime.rb:313:in `check_for_activated_spec!': You have already activated i18n 1.1.1, but your Gemfile requires i18n 1.1.0. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
这是当前激活的包和Gemfile中要求的不一样,可以更改Gemfile,也可以运行bundle update重新配置包
来源:oschina
链接:https://my.oschina.net/u/4277479/blog/3525140