简体   繁体   English

如何在不使用Rails的视图的情况下编程到模型DB的路由?

[英]How to program routes to a model DB without a view in ruby on rails?

I have a website that translates DNA codons to MRNA codons. 我有一个网站,可将DNA密码子翻译成MRNA密码子。 I created am immutable immutable 64 row DB model with 3 columns with sqlite, which I then preceeded to seed. 我用sqlite创建了一个具有3列的不可变的64行DB模型,然后开始进行播种。 Basically, what I want is the user to input a string into a text-area and break the string into 3 character string fragments "ttcttaatt..." --> (dnaCodon) [ttc][tta][att] --> that match to the database returning matched results (mrnaCodon ) [uuc] [uua] [auu]. 基本上,我想要的是用户在文本区域中输入一个字符串并将该字符串分成3个字符串片段“ ttcttaatt ...”->(dnaCodon)[ttc] [tta] [att]->返回数据库的匹配结果(mrnaCodon)[uuc] [uua] [auu]。 I created a search form on one of my static pages 我在其中一个静态页面上创建了一个搜索表单

I am running into trouble now with my routes in the search form. 我现在在搜索表单中的路线遇到了麻烦。 I want to query the search and then return it either to the same page, or a different page- whichever method is easier and makes more sense. 我想查询搜索,然后将其返回到同一页面或另一页面,无论哪种方法都更容易并且更有意义。 I initially created my DB without scaffolding because I don't want it accessible by users for alterations. 我最初创建数据库时没有使用脚手架,因为我不希望用户对其进行更改。 I ended up creating a scaffolding folder so I dunno why it's not connecting to the database. 我最终创建了一个脚手架文件夹,所以我不知道为什么它没有连接到数据库。 My page refreshes but does not return the DB query 我的页面刷新,但不返回数据库查询

app/controllers/aa_data_bases_controller.rb 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 this static page that the problem route is, in form_tag("/sequence"... When I run rails server the pages loads, i can input but page refreshes and nothing happens views / static_pages / sequence.html.erb问题路由就是在此静态页面中,在form_tag(“ / sequence” ...中。当我运行Rails Server时,页面可以加载,但是我可以输入但页面刷新没有发生

<% provide(:title, 'Sequence') %>
<h1>Sequence</h1>

<body>
    <div class="content-container-1" id="div3">
       <div class="container">
          <div class="row text-center">
            <h3>Add your 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") do %>
              <%= text_area_tag :search, params[:search], placeholder: "Translate DNA codons...", 
                  :size =>"75x10", class: "form-control"%>

              <% @aa_data_bases.each do |match| %>
              <li><%= match.mrnaCodon %></li>
              <% end %>

            </div>
        </div>
    </div>
</div>

config/routes.rb config / routes.rb

Rails.application.routes.draw do
  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'

  get 'aa_data_bases/index'

  get 'aa_data_bases/create'
end

model/aa_data_bases.rb 型号/aa_data_bases.rb

class AaDataBases < ActiveRecord::Base
 def self.search(query)
    where("%#{query}%") 
  end
end

DB D B

class CreateAaDataBases < ActiveRecord::Migration
  def change
    create_table :aa_data_bases do |t|
      t.string :aaFullName
      t.string :dnaCodon
      t.string :mrnaCodon

      t.timestamps
    end
  end
end

My seeding file, there are 64 entries just like this 我的种子文件,有64个条目,就像这样

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 for static pages app/controllers/static_pages_controller.rb 静态页面控制器app / controllers / static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end

  def contact
  end

  def seqresults
  end

end

app/controllers/sequence_page_controller.rb app / controllers / sequence_page_controller.rb

class SequencePageController < ApplicationController
    def sequence
    end
 end

I see a few problems here: 我在这里看到一些问题:

  1. The Form in your sequence view posts to the sequence URL with a GET, which has an empty action. 序列视图中的“表单”通过GET发送到序列URL,该GET具有空操作。 So it just renders the sequence view. 因此,它仅呈现序列视图。 This is the behavior you're seeing, and it is what you should expect based on how this is all written. 这是您所看到的行为,根据所有内容的编写方式,这是您应该期望的。

  2. I don't see any view here that would actually render the contents of @aa_data_bases. 我在这里看不到任何会实际呈现@aa_data_bases内容的视图。 What view have you built that will consume this controller variable and display the resulting set of codons? 您建立了一个将使用此控制器变量并显示密码子结果集的视图? Does such a view exist already? 这样的观点已经存在吗? If not, you will need to create one. 如果没有,则需要创建一个。

Some quick suggestions: 一些快速的建议:

  1. Your results are not static, so I'd move the sequence page out of the static pages controller. 您的结果不是静态的,因此我将序列页面移出了静态页面控制器。 You are probably going to want an integrated search/results page, so your users can easily do sequences of searches. 您可能需要集成的搜索/结果页面,以便您的用户可以轻松地执行搜索序列。 Start by shifting '/sequence' to the AaDataBases#index method. 首先将“ / sequence”转换为AaDataBases#index方法。 Add a basic index method that sets the value of @aa_data_bases to an empty array. 添加一个基本的索引方法,该方法将@aa_data_bases的值设置为一个空数组。 Optionally you can configure it to handle a search query parameter. (可选)您可以配置它以处理search查询参数。

  2. Move your current index method on the AaDataBasesController to a create method, so it can respond to POST requests 将您当前在AaDataBasesController上的索引方法移动到create方法,以便它可以响应POST请求

  3. Change the sequence view you've created to display a result set based on @aa_data_bases if it isn't empty, and update the form to use POST. 更改您创建的序列视图以显示基于@aa_data_bases的结果集(如果不为空),并更新表单以使用POST。

  4. Make sure your routes include both the index and create routes. 确保您的路线同时包含indexcreate路线。

These changes should get you to a functional set of pages. 这些更改将使您进入一组功能页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM