Ruby Sinatra向用户显示所有数据(自定义词典和定义应用程序),而不是特定单词的数据
正在尝试创建用户自定义词典。在索引处,用户将看到其所有自定义单词的列表,并且能够添加更多。然后,他们可以单击某个单词上特定页面的链接,在该链接中,用户将显示其定义,并可以添加更多内容 我的问题是显示所有单词的定义 下面是我的ruby代码Ruby Sinatra向用户显示所有数据(自定义词典和定义应用程序),而不是特定单词的数据,ruby,sinatra,Ruby,Sinatra,正在尝试创建用户自定义词典。在索引处,用户将看到其所有自定义单词的列表,并且能够添加更多。然后,他们可以单击某个单词上特定页面的链接,在该链接中,用户将显示其定义,并可以添加更多内容 我的问题是显示所有单词的定义 下面是我的ruby代码 class Word @@words = [] define_method(:initialize) do |word| @word = word @id = @@words.length(
class Word
@@words = []
define_method(:initialize) do |word|
@word = word
@id = @@words.length().+(1)
@definitions = []
end
define_singleton_method(:all) do
@@words
end
define_method(:save) do
@@words.push(self)
end
define_singleton_method(:clear) do
@@words = []
end
define_method(:id) do
@id
end
define_singleton_method(:find) do |ident|
found_word = nil
@@words.each() do |word|
if word.id.eql?(ident)
found_word = word
end
end
found_word
end
define_method(:add_definition) do |definition|
@definitions.push(definition)
end
define_method(:definitions) do
@definitions
end
define_method(:word) do
@word
end
end
class Definition
@@definitions = []
define_method(:initialize) do |definition, part|
@definition = definition
@part = part
@id = @@definitions.length().+(1)
end
define_singleton_method(:all) do
@@definitions
end
define_method(:save) do
@@definitions.push(self)
end
define_singleton_method(:clear) do
@@definitions = []
end
define_method(:id) do
@id
end
define_singleton_method(:find) do |ident|
found_definition = nil
@@definitions.each() do |definition|
if definition.id.eql?(ident)
found_definition = definition
end
end
found_definition
end
define_method(:part) do
@part
end
define_method(:definition) do
@definition
end
end
这是我的sinatra页面
require('sinatra')
require('sinatra/reloader')
also_reload('lib/**/*.rb')
require('./lib/definition')
require('./lib/word')
get('/') do
@words = Word.all()
erb(:index)
end
post('/') do
word = params.fetch('word_input')
new_word = Word.new(word)
new_word.save()
@words = Word.all()
erb(:index)
end
get('/word/:id') do
@words = Word.all()
@definitions = Definition.all
@definition = Definition.find(params.fetch('id').to_i())
erb(:word_page)
end
post('/word/:id') do
definition = params.fetch('definition_input')
part = params.fetch('part_input')
new_definition = Definition.new(definition, part)
new_definition.save
erb(:success)
end
这是我的页面,我显示所有单词的所有定义
<h1>Your defnitions for your word.</h1>
<p>Here you can view your definitions for your word and add more</p>
<% @definitions.each() do |definition| %>
<ul>
<li><strong>Part:</strong> <%= definition.part() %>
<br>
<strong>Definition:</strong> <%= definition.definition()%></li>
</ul>
<% end %>
<form action="/word/:id" method="post">
<label for="definition_input">New definition</label>
<input id="definition_input" name="definition_input" type='text'>
<br>
<label for="part">New part (E.g. Verb, subj, noun)</label>
<input id='part_input' name="part_input" type="text">
<br>
<button type="submit">Add defintion and the part</button>
</form>
<a href="/">Return to home page</a>
你对你的词的定义。
在这里,您可以查看您的单词定义并添加更多内容
- 部分:
定义:
新定义
新部分(例如动词、主语、名词)
添加定义和零件
首先,我认为你应该清理你的代码,因为这将使你的一切变得更容易(如果你再次问这样的问题,阅读你的问题的人也可以)
看看你能不能把这段代码
define_singleton_method(:find) do |ident|
found_definition = nil
@@definitions.each() do |definition|
if definition.id.eql?(ident)
found_definition = definition
end
end
found_definition
end
进入这个
def self.find(ident)
@@definitions.each do |definition|
if definition.id == ident
return definition
end
end
nil
end
也不是很漂亮,你可以把它变成这样:
def self.find(ident)
@@definitions.find{|definition| definition.id == ident}
end
这并不能回答你的问题,但我认为编码风格很重要,所以我想指出这一点
你的问题是,在你的视图中,你正在调用
@definitions.each()做|定义|
您引用了sinatra服务器中的调用所填充的@definitions
,您需要引用@definition
,并将其嵌入到视图中,因为您不想显示所有定义。(您可能需要为此创建第二个视图)