Sinatra编程

来源:互联网 发布:什么是自动编程系统 编辑:程序博客网 时间:2024/05/18 01:45

Sinatra 编程

1)get/post/put的次序

(一)此段代码访问songs/new会进入songs/newget '/songs/new' do    @song = Song.new    slim :new_songendget '/songs/:id' do    @song = Song.find_by(id: params[:id])    slim :show_songend(二)此段代码访问songs/new会进入songs/:idget '/songs/:id' do    @song = Song.find_by(id: params[:id])    slim :show_songendget '/songs/new' do    @song = Song.new    slim :new_songend

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

require 'rubygems'require 'data_mapper' # requires all the gems listed aboverequire  'dm-migrations'DataMapper.setup(:default, 'sqlite:///path/to/project.db')class Category  has n, :categorizations  has n, :posts,      :through => :categorizationsendDataMapper.finalizeDataMapper.auto_migrate!

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

class Product < ApplicationRecord  self.table_name = "my_products"enduser = Product .create(name: "David", occupation: "Code Artist")users = Product .all