ruby - Sinatra displaying all data to user (custom dictionary and definition app) instead of data for specific word -
trying make user custom dictionary. @ index, user present list of of custom words , have ability add more. can click link specific page on word user shown definitions , can add more.
my problem definitions words displayed.
below ruby code..
class word @@words = [] define_method(:initialize) |word| @word = word @id = @@words.length().+(1) @definitions = [] end define_singleton_method(:all) @@words end define_method(:save) @@words.push(self) end define_singleton_method(:clear) @@words = [] end define_method(:id) @id end define_singleton_method(:find) |ident| found_word = nil @@words.each() |word| if word.id.eql?(ident) found_word = word end end found_word end define_method(:add_definition) |definition| @definitions.push(definition) end define_method(:definitions) @definitions end define_method(:word) @word end end class definition @@definitions = [] define_method(:initialize) |definition, part| @definition = definition @part = part @id = @@definitions.length().+(1) end define_singleton_method(:all) @@definitions end define_method(:save) @@definitions.push(self) end define_singleton_method(:clear) @@definitions = [] end define_method(:id) @id end define_singleton_method(:find) |ident| found_definition = nil @@definitions.each() |definition| if definition.id.eql?(ident) found_definition = definition end end found_definition end define_method(:part) @part end define_method(:definition) @definition end end
here sinatra page..
require('sinatra') require('sinatra/reloader') also_reload('lib/**/*.rb') require('./lib/definition') require('./lib/word') get('/') @words = word.all() erb(:index) end post('/') word = params.fetch('word_input') new_word = word.new(word) new_word.save() @words = word.all() erb(:index) end get('/word/:id') @words = word.all() @definitions = definition.all @definition = definition.find(params.fetch('id').to_i()) erb(:word_page) end post('/word/:id') definition = params.fetch('definition_input') part = params.fetch('part_input') new_definition = definition.new(definition, part) new_definition.save erb(:success) end
here page displaying definitions words
<h1>your defnitions word.</h1> <p>here can view definitions word , add more</p> <% @definitions.each() |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 , part</button> </form> <a href="/">return home page</a>
first of think should clean code since make lot easier (and in case ask question again people reading question).
see can turn piece of code
define_singleton_method(:find) |ident| found_definition = nil @@definitions.each() |definition| if definition.id.eql?(ident) found_definition = definition end end found_definition end
into this
def self.find(ident) @@definitions.each |definition| if definition.id == ident return definition end end nil end
not beautiful either, transform this:
def self.find(ident) @@definitions.find{|definition| definition.id == ident} end
now doesn't answer question though, think coding style important, wanted point out nevertheless.
the problem have inside of view calling
@definitions.each() |definition|
you reference @definitions
filled call in sinatra server, need reference @definition
, embed view since don't want display definitions. (you might want create second view this).
Comments
Post a Comment