1

这是我的控制器。def random_generate没有给我任何输出,当我创建对象时,长度在那里但random_primer_generated为空。有人能帮我吗?

class GeneratorsController < ApplicationController
  before_action :set_generator, only: [:show, :edit, :update, :destroy]
  helper_method :random_generate
  # 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 = length.to_i
        length.times { seq << chars[rand(chars.size)] }
       @generator.random_primer_generated = seq
    end
    return @generator.random_primer_generated
  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_VIEW(获取用户的数据 - 长度和选择):

<%= 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_random_primer_generated, 'Randomly')%>

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

3 回答 3

2

注意:这里的表格可能是一个红鲱鱼。如果您只希望在用户显式单击该按钮时随机生成代码,而不是在每次创建或保存后随机生成,那么您不需要任何回调。你做对了。您唯一缺少的是方法末尾的保存调用,并且您还希望将用户重定向回页面。

但是,如果您确实希望在每次创建或更新后进行生成,那么您可能应该将 button_to 调用更改为 f.submit,并将 random_generate 代码移动到您的模型中。然后,您可以对 after_save 使用 rails 回调,并在控制器中从那里调用该方法。

更多信息: http: //guides.rubyonrails.org/active_record_callbacks.html

于 2013-06-25T17:59:47.777 回答
1

您没有在任何地方调用您的 helper_method。当您发布表单并到达服务器时,会调用 create 方法。不会触发 helper_method,因此您不会创建随机数。

我不太了解它,但是您要随机化什么?例如,您为什么不简单地使用 rand(999) 来随机化数字?

于 2013-06-25T17:35:31.530 回答
1

看起来您想random_generate在 new 和 create 操作之后调用,但您没有调用它。您所要做的就是更改helper_method :random_generateafter_action :random_generate, only: [:create, :new]

您还需要确保将新记录保存在random_generate. 你不需要return @genator.random_primer_generated. 见下文:

def random_generate
  if @generator.choice == 'Randomly'
    length = @generator.primer_length
    chars = 'atgc'
    seq = ''
    #length = length.to_i
    length.times { seq << chars[rand(chars.size)] }
   @generator.random_primer_generated = seq
  end
  @generator.save!
end
于 2013-06-25T17:37:25.163 回答