zoukankan      html  css  js  c++  java
  • rails再体验(第一个程序)

    掌握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,在RailsInstallerRuby2.1.0lib ubygems2.1.0gems ailties-4.2.6lib ailsgenerators ailsapp emplates目录下修改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:5 CreateCode emp ailstutorialfirst_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:5 CreateCode emp ailstutorialfirst_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

    同时修改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进行了升级:

    gem update –system

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

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

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

    Snap1

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

    注意rails generate生成的appmodelsuser.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: 对象不支持此属性或方法

    Rails.root: E:/05 Create/Code/temp/railstutorial/test_app

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

  • 相关阅读:
    CATransition
    OC中只有重写没有重载
    OC内存管理
    NSNotification
    关于一个小应用软件的思考
    tableView主从表在storyboard连线是 Selcetion Segue和Accessory Action的区别
    关于ios8模拟器不能输入中文问题以及软键盘不弹出问题
    UINavigation Bar中使用UIcollectionView,在UIcollectionView的顶端和低端出现空白的问题
    升级到Xcode6.2后 免证书真机调试出错的问题
    让UITableView 的 headerView跟随 cell一起滚动,tableHeaderView
  • 原文地址:https://www.cnblogs.com/lustforlife/p/6260498.html
Copyright © 2011-2022 走看看