未定义的方法'random_generate'用于#<generator:0x4cce798> </generator:0x4cce798>

时间:2013-06-27 10:04:02

标签: ruby-on-rails-3

我有一个方法,我想从我的控制器打电话。我在堆栈中读到的是他们中的大多数据说包含applicationController中的方法。因此我做了。但我仍然得到以下错误。 some1可以帮帮我吗?铁路新手

应用程序控制器:

helper_method :random_generate

   def random_generate
     length = @generator.primer_length
        chars = 'ATGC'
        seq = ''
        length.times { seq << chars[rand(chars.size)] }
       @generator.random_primer_generated= seq
    end 

CONTROLLER:

class GeneratorsController < ApplicationController
  before_action :set_generator, only: [:show, :edit, :update, :destroy]
  after_action :generate_option, only: [:create,:edit]
  helper_method :random_primer_generated
  # 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 generate_option
    if params[:choice] == 'Randomly'
        @generator.random_generate
    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',generate_option_generator_path(@generator))%>
<% end %>

1 个答案:

答案 0 :(得分:2)

如果您想在random_generate上致电@generator,则必须在Generator课程中实施该方法。您所做的是在ApplicationController中实现的,因此您必须在不引用@generator的情况下调用它。

另外,我认为只有在视图中调用helper_method :random_generate作为控制器方法时,才需要调用random_generate