How to program routes to a model DB without a view in ruby on rails? -
i have website translates dna codons mrna codons. created immutable immutable 64 row db model 3 columns sqlite, preceeded seed. basically, want user input string text-area , break string 3 character string fragments "ttcttaatt..." --> (dnacodon) [ttc][tta][att] --> match database returning matched results (mrnacodon ) [uuc] [uua] [auu]. created search form on 1 of static pages
i running trouble routes in search form. want query search , return either same page, or different page- whichever method easier , makes more sense. created db without scaffolding because don't want accessible users alterations. ended creating scaffolding folder dunno why it's not connecting database. page refreshes not return db query
app/controllers/aa_data_bases_controller.rb
class aadatabases < applicationcontroller def index @aa_data_bases = aadatabases.new end def create if params[:search] @aa_data_bases = aadatabases.search(params[:search]).order("created_at desc") else @aa_data_bases = aadatabases.all.order('created_at desc') end end
end
views/static_pages/sequence.html.erb it's in static page problem route is, in form_tag("/sequence"... when run rails server pages loads, can input page refreshes , nothing happens
<% provide(:title, 'sequence') %> <h1>sequence</h1> <body> <div class="content-container-1" id="div3"> <div class="container"> <div class="row text-center"> <h3>add sequence here</h3> </div> <div class="row text-center pt mb"> <div class="col-md-6 col-md-offset-3"> <br> <%= form_tag("/sequence", :method => "aadatabases#index", id: "search-form") %> <%= text_area_tag :search, params[:search], placeholder: "translate dna codons...", :size =>"75x10", class: "form-control"%> <% @aa_data_bases.each |match| %> <li><%= match.mrnacodon %></li> <% end %> </div> </div> </div> </div>
config/routes.rb
rails.application.routes.draw root 'static_pages#home' match '/about', to: 'static_pages#about', via: 'get' match '/contact', to: 'static_pages#contact', via: 'get' match '/help', to: 'static_pages#help', via: 'get' match '/seqresults', to: 'static_pages#seqresults', via: 'get' match '/sequence', to: 'sequence_page#sequence', via: 'get' 'aa_data_bases/index' 'aa_data_bases/create' end
model/aa_data_bases.rb
class aadatabases < activerecord::base def self.search(query) where("%#{query}%") end end
db
class createaadatabases < activerecord::migration def change create_table :aa_data_bases |t| t.string :aafullname t.string :dnacodon t.string :mrnacodon t.timestamps end end end
my seeding file, there 64 entries this
aadatabases.create(aafullname: "phenylalanine", dnacodon:"ttt", mrnacodon:"uuu") aadatabases.create(aafullname: "phenylalanine", dnacodon:"ttc", mrnacodon:"uuc") aadatabases.create(aafullname: "leucine", dnacodon:"tta", mrnacodon:"uua") aadatabases.create(aafullname: "leucine", dnacodon:"ttg", mrnacodon:"uug") aadatabases.create(aafullname: "leucine", dnacodon:"ctt", mrnacodon:"cuu") aadatabases.create(aafullname: "leucine", dnacodon:"ctc", mrnacodon:"cuc") aadatabases.create(aafullname: "leucine", dnacodon:"cta", mrnacodon:"cua")
controller static pages app/controllers/static_pages_controller.rb
class staticpagescontroller < applicationcontroller def home end def end def end def contact end def seqresults end end
app/controllers/sequence_page_controller.rb
class sequencepagecontroller < applicationcontroller def sequence end end
i see few problems here:
the form in sequence view posts sequence url get, has empty action. renders sequence view. behavior you're seeing, , should expect based on how written.
i don't see view here render contents of @aa_data_bases. view have built consume controller variable , display resulting set of codons? such view exist already? if not, need create one.
some quick suggestions:
your results not static, i'd move sequence page out of static pages controller. going want integrated search/results page, users can sequences of searches. start shifting '/sequence' aadatabases#index method. add basic index method sets value of @aa_data_bases empty array. optionally can configure handle
search
query parameter.move current index method on aadatabasescontroller create method, can respond post requests
change sequence view you've created display result set based on @aa_data_bases if isn't empty, , update form use post.
make sure routes include both
index
,create
routes.
these changes should functional set of pages.
Comments
Post a Comment