rails再体验(第一个程序)

 2023-09-10 阅读 21 评论 0

摘要:掌握redmine plugin开发的目标在2016年未实现,2017年继续。 选择《Ruby on Rails Tutorial》教程,windows安装railsinstaller,该版本ruby为V2.1.8,和bitnami redmine-3.3.0版本一致。但rails版本为4.2.5.1,和redmine对应的4.2.6不一致。首

掌握redmine plugin开发的目标在2016年未实现,2017年继续。

选择《Ruby on Rails Tutorial》教程,windows安装railsinstaller,该版本ruby为V2.1.8,和bitnami redmine-3.3.0版本一致。但rails版本为4.2.5.1,和redmine对应的4.2.6不一致。首先进行一次gem包更新:

  1. 启动终端。注意使用Railsinstall安装后自带的终端,终端里会设置环境。
  2. 修改gem源: bundle config 'mirror.http://mirrors.aliyun.com/rubygems/' 'http://gems.ruby-china.org/'  # 2017.1.7 发现gems.ruby-china.org好用。
  3. gem install rails –version=4.2.6 –no-ri –no-rdoc

根据书中1.2.3章节在命令终端生成第一个程序:  rails new first_app, 出现以下错误:

run  bundle install
Fetching source index from http://mirrors.aliyun.com/rubygems/
Net::HTTPNotFound

进入第一个程序的根目录: cd first_app

什么是程序,重新运行bundle install,错误提示https://rubygems.org连接时认证错误,网上查询需要生成SSL认证,比较麻烦。直接修改first_app下的Gemfile:

source 'https://rubygems.org'

修改为:

source 'http://gems.ruby-china.org/'

更彻底解决的方法是让rails生成的Gemfile文件里就修改source,在RailsInstaller\Ruby2.1.0\lib\ruby\gems\2.1.0\gems\railties-4.2.6\lib\rails\generators\rails\app\templates目录下修改Gemfile文件(同时修改source源和 tzinfo-data版本),这样rails new创建的Gemfile就ok了。

再次运行bundle install,显示以下错误:

什么是程序设计??Using activesupport 4.2.6

Bundler::GemspecError: Could not read gem at D:/App/Coder/RailsInstaller/Ruby2.1
.0/lib/ruby/gems/2.1.0/cache/tzinfo-data-1.2016.10.gem. It may be corrupted.
Installing loofah 2.0.3
Installing mail 2.6.4
Using rails-deprecated_sanitizer 1.0.3
Installing globalid 0.3.7
Using activemodel 4.2.6
Installing jbuilder 2.6.1
An error occurred while installing tzinfo-data (1.2016.10), and Bundler cannot
continue.
Make sure that `gem install tzinfo-data -v '1.2016.10'` succeeds before
bundling.

按照提示安装gem:

E:\05 Create\Code\temp\railstutorial\first_app>gem install tzinfo-data -v '1.201
6.10' --no-ri --no-rdoc
ERROR:  Error installing tzinfo-data:
        invalid gem: package is corrupt, exception while verifying: undefined me
thod `size' for nil:NilClass (NoMethodError) in D:/App/Coder/RailsInstaller/Ruby
2.1.0/lib/ruby/gems/2.1.0/cache/tzinfo-data-1.2016.10.gem

看起来目前版本不支持1.2016.10版本,换个低点的版本:

E:\05 Create\Code\temp\railstutorial\first_app>gem install tzinfo-data -v '1.201
5.6' --no-ri --no-rdoc
Fetching: tzinfo-data-1.2015.6.gem (100%)
Successfully installed tzinfo-data-1.2015.6
1 gem installed

一个c程序?同时修改Gemfile中的版本:

gem 'tzinfo-data',platforms: [:mingw, :mswin, :x64_mingw, :jruby]

修改为:

gem 'tzinfo-data', '1.2015.6' ,platforms: [:mingw, :mswin, :x64_mingw, :jruby]

再次bundle install运行成功,安装了56个gems。

因为版本已经和教程中的差异比较大(教程为ruby 2.0.0,rails 4.0.0),中间顺便对gem和bundle进行了升级:

第一个c程序、gem update –system

bundle update # 因为上面升级了Rails gem版本,所以必须要执行

bundle install   # 再次安装所需的gem,其实已没有更新

启动rails server: rails server。 通过http://127.0.0.1:3000可查看第一个程序。

Snap1

 

ruby falls,20170108继续。 经过上述折腾后,根据教程2.2内容, 增加User资源:rails generate scaffold User name:string email:string出错,而且没有任何提示。最后的解决方法是使用rails new app重新生成应用,再运行上述指令就好了,猜测和rails new app后修改了Gemfile里gem版本有关系。

注意rails generate生成的app\models\user.rb内容如下:

 class User < ActiveRecord::Base

end

看起来没有模型没有任何属性,一开始以为又是rails出了问题。根据网上内容,发现就该如此(数据库会正常创建属性,但是模型类就不包含该属性。刷新了我对ORM的认识)。

完成数据库迁移(bundle exec rake db:migrate)后启动服务器(rails s),通过客户端访问创建的Users(http://127.0.0.1:3000/users)又出错了

Showing E:/05 Create/Code/temp/railstutorial/test_app/app/views/layouts/application.html.erb where line #6 raised:

TypeError: 对象不支持此属性或方法

laravel手册。Rails.root: E:/05 Create/Code/temp/railstutorial/test_app

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb__1028411780_48770724'

继续搜索,找到答案,该文提供了3种解决方案。我用了第一种(删除了app\assets\javascripts\application.js文件中最后一行"//= require_tree .")解决,第3种方式看起来是根本解决,但对我的电脑无效。修改后访问正常:

 

转载于:https://www.cnblogs.com/lustforlife/p/6260498.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/37489.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息