2013-06-29 56 views
1

嗨,人們,我有一個關於params [:價值]的問題。是參數[:值]模型變量或可以是本地以及

我的模型必須包含稱爲值的變量屬性只有我可以在我的控制器中使用這個params [:value]?

我試圖從我的VIEW中獲取數據,但變量值只會用於1的函數而已。所以我想,如果有另一種方法可以做。

我認爲[這個問題是關係到第二radio_button =向數ATGC指定]:

<%= 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 , min: 3 %> 
    </div> 

    <label>Selection :</label><br> 
<table> 
    <thead> 
     <tr> 
     <th><label>Randomly</label> 
     <%= radio_button_tag(:choice, 'Randomly', true)%></th> 

     <th><label>Number of ATGC specified</label> 
     <%= radio_button_tag(:choice,'No_of_ATGC')%> </th> 
    </tr> 
    </thead> 

    <tbody> 
     <tr> 
      <td></td> 
      <td><label>Number of A :</label> 
      <%= number_field_tag 'No_of_A'%><br> 
      <label>Number of T :</label> 
      <%= number_field_tag 'No_of_T'%><br> 
      <label>Number of G :</label> 
      <%= number_field_tag 'No_of_G'%><br> 
      <label>Number of C :</label> 
      <%= number_field_tag 'No_of_C'%><br></td> 
     </tr> 
    </tbody> 
</table>  

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

在GeneratorController

class GeneratorsController < ApplicationController 
    before_action :set_generator, only: [:show, :edit, :update, :destroy] 
    after_action :generate_option, only: [:create, :update] 
    # 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 } 
     #random_generate 
     #@generator.save! 
     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 
    @generator.choice = params[:choice] 
    if params[:choice] == 'Randomly' 
     random_generate 
    elsif params[:choice] == 'No_of_ATGC' 

    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 

在ApplicationController中:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    def random_generate 
    length = @generator.primer_length 
     chars = 'ATGC' 
     seq = '' 
     length.times { seq << chars[rand(chars.size)] } 
     @generator.random_primer_generated = seq 
    end 

    def no_ATGC 

    counts = {'a'=>no_of_a,'t'=>no_of_a,'g'=>no_of_a,'c'=>no_of_a} 
    @generator.random_primer_generated = Bio::Sequence::NA.randomize(counts) 

    end 

end 

基本上這就是我需要做的。我需要知道有多少個A的T的G和C,以便爲用戶生成某些東西。另外我不確定是否可以在Rails中使用寶石Bioruby〜我不必擔心在RUby中太多,但Rails架構很難。我在gemfile中定義了寶石Bioruby。希望它有效。

+0

請問你可以把這裏的一些代碼作爲例子嗎? – Muntasim

+0

我更新了內容。嗨Muntasim:我決定不使用該jQuery,但首先表格視圖。稍後會做增強。 – BC2

回答

1

如果您嘗試使用參數創建/更新模型項目,模型會受到關注。您可以將任何參數作爲參數傳遞給控制器​​,但是如果您創建/更新模型項目,參數必須位於模型中。

+0

OK注意到...在我的情況下,我需要變量在我的模型中,因爲我需要創建。非常感謝。 – BC2

1

params包含從提交的表單中收到的信息。所以,這實際上取決於你是否在你的表單中有一個名字爲「value」的字段(編輯框,選擇框等)。因爲大多數Rails表單都是從Model生成的,通常這意味着你的模型中有一個屬性「value」。

相關問題