2013-06-27 60 views
0

我有一個方法,我想從我的控制器調用。我在棧中讀到的大部分都是在applicationController中包含了這個方法。因此我做了。但仍然有以下錯誤。有人能幫助我嗎?新建軌道未定義的方法'random_generate'爲#<發生器:0x4cce798>

應用控制器:

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 

控制器:

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 

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

<%= button_to('Generate',generate_option_generator_path(@generator))%> 
<% end %> 
+0

它只是我或你不應該'random_generate'而不是'random_primer_generated' – David

回答

2

如果你想打電話random_generate@generator,你必須實現該方法在Generator類中。你所做的是在ApplicationController中實現它,所以你必須在不參考@generator的情況下調用它。

此外,我認爲只有當您想在視圖中調用random_generate作爲控制器方法時才需要調用helper_method :random_generate

+0

我該如何實現該方法到我的生成器類? (我的意思是我應該把他們放在哪裏?)。 這就是爲什麼他們說如果我們在ApplicationController中放置一個方法,我們實際上是讓所有的CONTROLLERS都可以訪問這個方法? – BC2

+0

我試圖在我的VIEW中實際調用random_generate。我想將我的值傳遞給RANDOM_GENERATE方法,但是因爲我有很多選擇,可以通過random_generate或其他函數生成,但我沒有在我的代碼中包含這些函數。 – BC2

+1

問題是'@ generator.random_generate'在'@ generator'上調用方法,但是你在控制器上定義了方法。要麼不用'@ generator'調用它,要麼在'Generator'模型類中實現該方法。 – mario

相關問題