Sinatra编程

Sinatra 编程

1)get/post/put的次序

(一)此段代码访问songs/new会进入songs/new
get '/songs/new' do
    @song = Song.new
    slim :new_song
end

get '/songs/:id' do
    @song = Song.find_by(id: params[:id])
    slim :show_song
end
(二)此段代码访问songs/new会进入songs/:id
get '/songs/:id' do
    @song = Song.find_by(id: params[:id])
    slim :show_song
end
get '/songs/new' do
    @song = Song.new
    slim :new_song
end

2)datamapper使用
参见http://datamapper.org/getting-started.html

require 'rubygems'
require 'data_mapper' # requires all the gems listed above
require  'dm-migrations'

DataMapper.setup(:default, 'sqlite:///path/to/project.db')

class Category
  has n, :categorizations
  has n, :posts,      :through => :categorizations
end

DataMapper.finalize

DataMapper.auto_migrate!

3)activerecord使用
参见https://ruby-china.github.io/rails-guides/active_record_basics.html

class Product < ApplicationRecord
  self.table_name = "my_products"
end

user = Product .create(name: "David", occupation: "Code Artist")
users = Product .all

4) rackup config.ru跨域问题
参见 https://github.com/cyu/rack-cors

gem install rack-cors
config.ru中添加
require "rack/cors"
use Rack::Cors do
  allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值