【问题标题】:Link shorthand application in SinatraSinatra 中的链接速记应用程序
【发布时间】:2023-03-11 10:13:01
【问题描述】:

这是我的第一个 Sinatra 项目 - 链接缩短器,但我遇到了一些错误,老实说,sinatra 的内置调试器实际上什么也没告诉我。我希望你给我一个线索或提出解决问题的方法。

http://min.us/mkBIVTh7p - 截图,当我提交带有 url 的表单时发生这种情况:http://google.com 和 word 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>

【问题讨论】:

  • “遇到一些错误”——显示你的错误。我们还能如何帮助您?你甚至没有说问题是什么。

标签: ruby sinatra datamapper


【解决方案1】:

这是因为您需要完成模型。请参阅“完成模型”标题下的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多