王筝的博客
ruby学习

channel_type对应多种类型,每个类型在数据库中存储的字段和在页面上显示的字段是不同的,

如果在index里直接使用 select_tag 方法,是这样的:

<%= select_tag "channel_type", options_for_select([['信号源', 'signal_source'],
['直播轮播', 'live_and_rotate'],
['我的','my_channel'],
['cibn频道','cibn_channel']],@launcher_channel.channel_type) %>

不过这个下拉菜单显得有些多余,

可以在model里加一个方法,

  def channel_type_human
    {   
      signal_source:  '信号源',
      live_and_rotate:  '直播轮播',
      my_channel:  '我的',
      cibn_channel:  'cibn频道'
    }[channel_type.to_sym]
  end 

再 view 中,直接调用 channel.channel_type_human 就可以啦

https://github.com/thoughtbot/shoulda-matchers

 

加了gem ‘shoulda-matchers’, ‘~> 3.0’,

报错是因为没有配置spec/rails_helper.rb,详细内容在上面连接里的configuration里

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec
    with.library :rails
  end 
end

 

http://stackoverflow.com/questions/5286117/incompatible-character-encodings-ascii-8bit-and-utf-8

http://stackoverflow.com/questions/22932282/gemloaderror-specified-mysql2-for-database-adapter-but-the-gem-is-not-loade

这个是因为在Gemfile和database.yml里写的都是mysql,

应该是mysql2.

修改之后报出新错误:“

Gem::LoadError Specified ‘mysql2′ for database adapter, but the gem is not loaded. Add `gem ‘mysql2’` to your Gemfile

配置一下mysql2 的版本

If you have this error when upgrading to rails 4.2.4 (also with rails 4.1.5) try using this version of mysql2:

gem 'mysql2', '~> 0.3.18'

推荐两个网址:

https://github.com/rspec/rspec-rails

http://rspec.info/documentation/

上传了一个api的单元测试,https://github.com/shiralwz/grape_api_rspec

主要步骤:

(1)在Gemfile里加入新gem

gem 'rspec-rails', '~> 3.0'

(2)Download and install by running

bundle install

(3)Initialize the spec/ directory (where specs will reside) with

rails generate rspec:install

运行这个命令会自动生成以下三个文件

  • .rspec
  • spec/spec_helper.rb
  • spec/rails_helper.rb

(4)新增单元测试的代码

(5)执行

bundle exec rspec  spec/...

============================

对api的单元测试代码

require 'rails_helper'

RSpec.describe "helloAPI",type: :request do
  describe "GET the /api/hello" do
    it 'should return correct response via GET' do
      get '/api/hello', name: 'Mike'
      expect(response).to be_success
      expect(response).to have_http_status(200)
      body = JSON.parse(response.body)
      body['message'] == 'Hello Mike via GET'
    end 
  end 

  describe "POST the /api/hello" do
    it 'should return correct response via POST' do
      post '/api/hello', name: 'Mike'
      expect(response).to be_success
      expect(response).to have_http_status(200) //这里由于在api里设置了200所以检测200,默认是201
      body = JSON.parse(response.body)
      body['message'] == 'Hello Mike via POST'
    end 
  end 
end

 

 

 

Grape,为API设计而生DSL(Domain Specified Language),官网地址: http://intridea.github.io/grape

http://intridea.github.io/grape/docs/index.html

(1)描述性的API构建方式,代码可读性非常强

(2)提供了构建Restful API的一套工具,如参数约束,路径约束,版本管理等

(3)提供了JSON/XML/TXT格式的渲染工具

demo的地址是https://github.com/shiralwz/grape_api

module HelloApi
  require 'grape'

  class OneAPI < Grape::API
    resource :apis do
      format :json

      get 'hello' do
        {message: "hello #{params[:name]} via GET"}
      end 

      post 'hello' do
        {message: "hello #{params[:name]} via POST"}
      end 
    end 

  end 

end
Rails.application.routes.draw do

  mount HelloApi::OneAPI => '/' 
end

rails s

localhost:3000/apis/hello.json?name=mike

如果不需要apis这个命名空间可以直接删除 app/model/hello_api.rb里的第五行 resource :apis do 和第十五行 end