未显示错误,但索引中的返回结果为空

时间:2013-12-13 14:33:39

标签: ruby-on-rails

我正在尝试创建一个属于Generator的结果。为了创建Result,我在Generator show.html.erb中执行了代码,并在Generator的show.html.erb中渲染表单(Result)。但是,单击子项按钮时,不会调用Result方法。我怎么解决这个问题?没有显示任何错误,所以我不知道哪里出错了。当我在控制台中键入Result.all时,数据库中也没有记录。我遵循将第二个对象与第一个对象关联的方式,但仍然无法使它们正确。请帮帮我。

记录(不知道为什么会这么久。肯定会出现问题)

Started GET "/users/1/generators/new" for 127.0.0.1 at 2013-12-13 23:36:51 +0800
Processing by GeneratorsController#new as HTML
  Parameters: {"user_id"=>"1"}
  [1m[35mUser Load (2.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  Rendered generators/_form.html.erb (77.0ms)
  Rendered generators/new.html.erb within layouts/application (82.0ms)
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Completed 200 OK in 256ms (Views: 179.0ms | ActiveRecord: 15.0ms)


Started POST "/users/1/generators" for 127.0.0.1 at 2013-12-13 23:36:56 +0800
Processing by GeneratorsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"SBzLTWzzkziEGPDL7BZFnxdKuVeBJXQXUpKta3k5erQ=", "choice"=>"Randomly", "generator"=>{"primer_length"=>"10"}, "no_A"=>"", "no_T"=>"", "no_G"=>"", "no_C"=>"", "user_seq"=>"", "result_choice"=>"Yes", "commit"=>"Generate", "user_id"=>"1"}
  [1m[36mUser Load (2.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36m (2.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (3.0ms)[0m  INSERT INTO "generators" ("choice", "created_at", "f_primer", "melting_temp", "primer_length", "r_primer", "random_primer_generated", "result_choice", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"  [["choice", "Randomly"], ["created_at", Fri, 13 Dec 2013 23:36:56 MYT +08:00], ["f_primer", "GTGCTAAAAT"], ["melting_temp", 26.0], ["primer_length", 10], ["r_primer", "ATTTTAGCAC"], ["random_primer_generated", "GTGCTAAAAT"], ["result_choice", "Yes"], ["updated_at", Fri, 13 Dec 2013 23:36:56 MYT +08:00], ["user_id", 1]]
  [1m[36m (103.0ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/users/1/generators/5
Completed 302 Found in 136ms (ActiveRecord: 110.0ms)


Started GET "/users/1/generators/5" for 127.0.0.1 at 2013-12-13 23:36:56 +0800
Processing by GeneratorsController#show as HTML
  Parameters: {"user_id"=>"1", "id"=>"5"}
  [1m[35mUser Load (2.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mGenerator Load (2.0ms)[0m  [1mSELECT "generators".* FROM "generators" WHERE "generators"."id" = $1 LIMIT 1[0m  [["id", "5"]]
  [1m[35mResult Load (2.0ms)[0m  SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1  [["generator_id", 5]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  Rendered results/_form.html.erb (79.0ms)
  Rendered generators/show.html.erb within layouts/application (85.0ms)
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Completed 200 OK in 140ms (Views: 119.0ms | ActiveRecord: 14.0ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/generators.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/identities.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/results.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/welcome.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/generators.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/identities.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/registrations.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800


Started GET "/assets/results.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800


Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800


Started GET "/assets/welcome.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800


Started PUT "/users/1/generators/5?generator_id=5" for 127.0.0.1 at 2013-12-13 23:37:08 +0800
Processing by GeneratorsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"SBzLTWzzkziEGPDL7BZFnxdKuVeBJXQXUpKta3k5erQ=", "ncbi_ref_seq"=>"", "genome_seq"=>"TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG", "commit"=>"Analyze", "generator_id"=>"5", "user_id"=>"1", "id"=>"5"}
  [1m[36mUser Load (2.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mGenerator Load (2.0ms)[0m  SELECT "generators".* FROM "generators" WHERE "generators"."id" = $1 LIMIT 1  [["id", "5"]]
  [1m[36m (2.0ms)[0m  [1mBEGIN[0m
  [1m[35m (2.0ms)[0m  COMMIT
  [1m[36m (1.0ms)[0m  [1mBEGIN[0m
  [1m[35m (2.0ms)[0m  COMMIT
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
Redirected to http://localhost:3000/users/1/generators.5?id=5
Completed 302 Found in 27ms (ActiveRecord: 11.0ms)


Started GET "/users/1/generators.5?id=5" for 127.0.0.1 at 2013-12-13 23:37:08 +0800
Processing by GeneratorsController#index as 
  Parameters: {"id"=>"5", "user_id"=>"1"}
  [1m[35mUser Load (2.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mGenerator Load (2.0ms)[0m  SELECT "generators".* FROM "generators" WHERE "generators"."user_id" = $1  [["user_id", 1]]
  [1m[36mResult Load (1.0ms)[0m  [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m  [["generator_id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mResult Load (2.0ms)[0m  SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1  [["generator_id", 2]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mResult Load (2.0ms)[0m  [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m  [["generator_id", 3]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mResult Load (2.0ms)[0m  SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1  [["generator_id", 4]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mResult Load (2.0ms)[0m  [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m  [["generator_id", 5]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  Rendered generators/index.html.erb within layouts/application (61.0ms)
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m  [["id", 1]]
  [1m[35mCACHE (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Completed 200 OK in 128ms (Views: 111.0ms | ActiveRecord: 13.0ms)

生成器控制器

    def show 
  end

  # GET /generators/new
  def new
    @generator=Generator.new(params[:generator])
  end



def index
    #@generators = current_user.generators
    #@generators = @generators.order("created_at DESC")
  end
  def create    
    @generator = current_user.generators.build(generator_params)
    @generator.choice = params[:choice]
      if params[:choice] == 'Randomly'
          @generator.random_generate(generator_params)
      elsif params[:choice] == 'Specified ATGC'
          @generator.specified_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
      elsif params[:choice] == 'Seating'
          @generator.seating(params[:user_seq])
      end

    @generator.result_choice=params[:result_choice]
    @generator.save
    respond_to do |format|
       if @generator.result_choice == 'Yes'
              format.html { redirect_to(:action =>"show",:id =>@generator.id )}
       else
              format.html { redirect_to(user_generators_path(@generator,:user_id => current_user.id,:generator_id=>@generator.id) ) }
       end
     end 
  end

  # PATCH/PUT /generators/1
  # PATCH/PUT /generators/1.json
  def update
    respond_to do |format|
      if @generator.update(params[:generator])
        @generator.save
        format.html { redirect_to(user_generators_path(@generator,:user_id => current_user.id,:id=>@generator.id) ) }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @generator.errors, status: :unprocessable_entity }
      end
    end
  end

结果控制器

def new
  end

  # GET /results/1/edit

  # POST /results
  # POST /results.json TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG
  def create
    @result = @generator.build_result(result_params)
    @generator=@result.generate_result(result_params)
    @generator.result.save
    redirect_to user_generators_path
  end

在Generator show.html.erb

中呈现的结果表单
<%= form_for(@generator.build_result(params[:result]),:url =>user_generator_path(@generator,:user_id => current_user.id, :generator_id => @generator.id),:html =>{:method=>:put}) do |f| %>
<script>
$(document).ready(function(){
        $("#accession_number").click(function(){
            $("#FASTA").hide();
            $("#highlight").hide();
        })
        $("#FASTA").click(function(){
            $("#accession_number").hide();
            $("#highlight").hide();
        })    
        $("#reset").click(function(){
            $("#accession_number").show();
            $("#FASTA").show();
            $("#highlight").show();
        })
});
</script>

  <font size ="6">*** <u>Choose ONE of the input methods</u> *** </font>
  <p id="accession_number">
    <label><strong><font size ="3">NCBI accession number:</font></strong></label><br />
  <%= text_field_tag(:ncbi_ref_seq,nil,placeholder: 'Accession Number')%>
  </p>

  <p id="highlight"><font size ="5">
        OR  
  </font></p>

  <p id="FASTA">
    <strong><font size ="3">Paste your sequence here: ( FASTA format ) :</font></strong> 
    <%= text_area_tag(:genome_seq,nil,size: "50x10",placeholder: 'Input your sequence here')%>
 </p>

  <input type="reset" value="Reset" id="reset">
    <%= submit_tag 'Analyze' %>
<% end %>

生成器的表单(我需要使用标签,如果我使用f.button,我得到空的结果)

<html>
<script>
$(document).ready(function() {
 var total = 0;
 $.each($(':input'), function(){
   total += parseInt($(this).val(), 10); //use radix 10 to ensure correct parseInt val
 });
 $("span").text(i);
});
</script>
<body >
<%= form_for(@generator, :url => user_generators_path(:user_id => current_user.id, :id => @generator.id)) do |f| %>
        <% if @generator.errors.any? %>
            <div id="error_explanation">
              <h2><%= pluralize(@generator.errors.count, "error") %> prohibited this generator from being saved:</h2>

              <ul>
              <% @generator.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
              <% end %>
              </ul>
            </div>
        <% else %>
        <fieldset class ="primer">
        <legend><strong>Method Use :</strong></legend><br>  
            <h3 align="left"><font size ="5"><b>Step 1: <u>Choose only ONE of the methods</u></font></b></h3> 
        <table class="p_gen">
            <thead>
              <tr>
                <th class="method1"><label>Randomly</label><br />
                <%= radio_button_tag(:choice, 'Randomly',checked:true )%>
                <p> ( Generate a primer randomly )</p></th>

                <th class="method2"><label>Specified ATGC</label><br />
                <%= radio_button_tag(:choice,'Specified ATGC')%> 
                <p> ( Generate a primer with number of A,T,G and C )</p></th>

                <th class="method3"><label>Seating</label><br />
                <%= radio_button_tag(:choice,'Seating')%> 
                <p> ( Generate a primer according to your preference )</p></th>
             </tr>
            </thead>

            <tbody>
                <tr>
                <td class="method1">
                    <p> Input length of the primer you want : </p>
                    <label>Primer Length :</label>
                    <%= f.number_field(:primer_length , min: 6 , max: 35)%>
                </td>
                <td class="method2">
                    <p>Input the number of each base the primer should have</p>
                    <label>Number of A :</label>
                    <%= number_field_tag :no_A,nil, :min=>0, :max=>35 %><br />
                    <label>Number of T :</label>
                    <%= number_field_tag :no_T,nil, :min=>0, :max=>35 %><br />
                    <label>Number of G :</label>
                    <%= number_field_tag :no_G,nil, :min=>0, :max=>35 %><br />
                    <label>Number of C :</label>
                    <%= number_field_tag :no_C,nil, :min=>0, :max=>35 %><br />
                    Total bases:<span></span>
                </td>   
                <td class="method3">
                    <p> Input your preference sequence (only IUPAC nucleotide).</p>
                    <p><b><u>IUPAC Nucleotide :</u></b></p>
                    <p>A,T,G,C,R,Y,S,W,K,M,B,D,H,V,N </p>
                    Example: <br /> 
                    Preference primer = TAGGCT<b>N</b>TTA<b>N</b>GAC<b>N</b> <br />
                    N = Any base ( A/ T / G / C) <br /><br />               
                    <label>Desired sequence :</label><br>
                    <%= text_field_tag(:user_seq,nil,:maxlength => 35)%>
                </td>
                </tr>
            </tbody>
        </table>
        </fieldset>
        <br>

        <fieldset class ="sample">
        <h4><font size="5"><b>Step 2: <u>Choose 'Yes' if you want to input reference sequence for Binding-time analysis </font></u></b></h4> 
        <legend><strong>Do you have NCBI data to extract / FASTA file to input?</strong></legend><br>
            <label>Yes</label>
            <%= radio_button_tag(:result_choice,'Yes')%> 
            <label>No</label>
            <%= radio_button_tag(:result_choice,'No')%>
        <br>
        </fieldset>

        <br><div class = "button"> 
           <%=f.submit("Generate", :class => "Gbutton_class") %>
        </div>  
    <%end %>    
<%end%>
</body>
</html>

的routes.rb

   Project::Application.routes.draw do
  get "/signout" => "sessions#destroy", :as => :signout


  root :to => 'welcome#index' 
  get '/auth/:provider/callback' => 'sessions#create' 
  post '/auth/identity/callback' => 'sessions#create'

  resources :users do
    resources :generators 
    resources :results
  end  
  resources :identities

1 个答案:

答案 0 :(得分:0)

我不完全清楚为什么有两个生成器表单(可能我遗漏了一些东西),但在某些地方你使用的是单数用户:user_generators_path在其他地方你正在使用users_generators_path 1}}(注意复数用户)。通过rake routes检查哪个是正确的,并修正任何不正确的条目。