1

我想创建一个具有通过以下方法生成的随机序列的对象。但是在单击“生成”按钮后,它会创建 2 个对象(1 个具有所有属性为空,另一个具有我输入的值)。谁能告诉我问题出在哪里?

控制器 :

class GeneratorsController < ApplicationController
  before_action :set_generator, only: [:show, :edit, :update, :destroy]
  after_action :random_generate, only: [:create, :new, :edit]
  # GET /generators
  # GET /generators.json
  def index
    @generators = Generator.all
  end

  # GET /generators/1
  # GET /generators/1.json
  def show
  end

  # GET /generators/new
  def new
    @generator = Generator.new
  end

  # GET /generators/1/edit
  def edit
  end

  # POST /generators
  # POST /generators.json
  def create    
    @generator = Generator.new(generator_params)

    respond_to do |format|
      if @generator.save
        format.html { redirect_to @generator, notice: 'Generator was successfully created.' }
        format.json { render action: 'show', status: :created, location: @generator }
      else
        format.html { render action: 'new' }
        format.json { render json: @generator.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /generators/1
  # PATCH/PUT /generators/1.json
  def update
    respond_to do |format|
      if @generator.update(generator_params)
        format.html { redirect_to @generator, notice: 'Generator was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @generator.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /generators/1
  # DELETE /generators/1.json
  def destroy
    @generator.destroy
    respond_to do |format|
      format.html { redirect_to generators_url }
      format.json { head :no_content }
    end
  end

  def random_generate
    if @generator.choice == 'Randomly'
        length = @generator.primer_length
        chars = 'ATGC'
        seq = ''
        length.times { seq << chars[rand(chars.size)] }
       @generator.random_primer_generated = seq
    end
    @generator.save!
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_generator
      @generator = Generator.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def generator_params
      params.require(:generator).permit(:primer_length, :choice, :random_primer_generated)
    end
end

看法 :

<%= form_for (@generator ) 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>
  <% end %>

  <div class="field">
    <label>Primer Length</label><br>
       <%= f.number_field :primer_length %>
  </div>

    <label>Selection :</label><br>
    <label>Randomly</label>
    <%= radio_button_tag(:choice, 'Randomly')%>

<%= button_to('Generate', random_generate_generator_path(@generator))%>
<% end %>
4

1 回答 1

1

您有一个回调,每次显示视图时都会after_action触发该操作。这就是你的空对象的来源。你不应该需要这个回调 on 。random_generatenewafter_actionnew

于 2013-06-26T18:14:03.343 回答