在Sinatra链接速记应用程序

时间:2012-04-08 12:39:23

标签: ruby sinatra datamapper

这是我的第一个Sinatra项目 - 链接缩短但我遇到了一些错误,说实话,sinatra的内置调试器几乎没有告诉我。我希望你能给我一些线索或建议解决问题的方法。

http://min.us/mkBIVTh7p - 屏幕截图,当我使用网址提交表单时发生这种情况:http://google.com和google

require 'sinatra'
    require 'shotgun'
    require 'data_mapper'
    require 'dm-migrations'
    require 'dm-sqlite-adapter'

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/form.db")

class Url
    include DataMapper::Resource
    property :id, Serial
    property :url, String
    property :suggestion, String
end

get '/' do 
    erb :index 
end

post '/' do
    Url.create(:url => params[:url], :suggestion=> params[:suggestion])
end

get '/Url.suggestion' do
    query = request_path.slice!(0) 
    redirection = Url.first(:suggestion => query)
    redirect redirection.url
end

index.rb

<!doctype html>
<html>
<head>
    <title>Skracanie linków</title>
</head>
<body>

<form name="form" method="post" action="#">
    <fieldset>
        <legend>Wpisz co trzeba</legend>
        <p><label> <input type="text" name="post[url]"/>Url:</label></p>
        <p><label> <input type="text" name="post[suggestion]"/>Suggested name:</label></p>
    </fieldset>
    <p class="center">
        <input type="reset" value="Wyczyść formularz"/>
        <input type="submit" value="Wyślij"/>
    </p>  

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

这是因为您需要完成模型。请参阅“最终模型”标题下的http://datamapper.org/getting-started.html

在定义模型后添加finalize命令:

class Url
    include DataMapper::Resource
    property :id, Serial
    property :url, String
    property :suggestion, String
end

# add this line
DataMapper.finalize