2012-05-02 94 views
0

我有2個視圖要在創建操作之前使用。查找ID找不到ID並創建

第一視圖,是add_sample_details.html.erb

<% form_tag add_expt_details_samples_path :method => :post do %> 
    <% for sample in @samples %> 
     <% fields_for "samples[]", sample do |form| %> 
      <fieldset> 
       <legend>Sample Name: <%= sample.name %></legend> 
       <p><center><%= form.label :sample_title %> 
       <%= form.text_field :sample_title, :size => 25 %></center></p> 
       <table> 
        <tr> 
         <td> 
          <%= form.label :taxon_id %> 
          <%= form.text_field :taxon_id, :size => 15 %> 
         </td> 
         <td> 
          <%= form.label :scientific_name %> 
          <%= form.text_field :scientific_name, :size => 20 %> 
         </td> 
         <td> 
          <%= form.label :common_name %> 
          <%= form.text_field :common_name, :size => 15 %> 
         </td> 
        </tr> 
       </table> 
       <p> 
        <center> 
         <%= form.label :sample_descripition %><br \> 
         <%= form.text_area :description %> 
        </center> 
       </p> 
      </fieldset> 
     <% end %> 
    <% end %> 
    <p><center><%= submit_tag "Next" %></center></p> 

    <% for samp in @samples %> 
     <%= hidden_field_tag("sample_ids[]", samp.id) %> 
    <% end %> 
<% end %> 
在SamplesController

,該add_expt_details看起來像這樣

def add_expt_details 
    # Collect the samples that were sent 
    @expt_samples = Sample.find(params[:sample_ids]) 
    end 

和add_expt_details.html.erb看起來像這樣

<% form_for @expt_samples, :url => create_study_samples_path, :html => { :method => :put } do |f| %> 
    <% @expt_samples.each do |samp|%> 
     <% f.fields_for :expts do |form| %> 
     <%= form.label :experiment_title %> 
     <%= form.text_field :expt_title, :size => 15 %><br \> 
     <% end %> 
    <% end %> 
    <p><center><%= submit_tag "Next" %></center></p> 
<% end %> 

和我的create_study動作是這樣的

def create_study 
    @study = Study.new(params[:study]) 
    # this method collects all the samples from the add_sra_details view from the hidden_field_tag 
    if current_user.id? 
     # Put the current user_id in the study.user_id 
     @study.user_id = current_user.id  
     if @study.save # Save the values for the study 
      # Update the Sample attributes 
      @samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? } 
      # For all those sample_ids selected by the user, put the study ids in the sample_study_id 
      @samples.each do |samp|  
      @study.samples << samp   
      end  

      # get the ids_sent from the add_expt_details 
      @expt_samples = Sample.find(params[:id])    

      # A Flash message stating the details would be prefereable..! (Means you have to do it) 
      flash[:success] = "Your Study has been Created"   
      redirect_to add_expt_details_samples_path 
     else 
      flash[:error] = "Study Faulty" 
      render :action => "add_sra_details" 
     end 
    end 
    end 

該錯誤消息,我得到當我保持@expt_samples = Sample.find(PARAMS [:ID])爲 「用於無未定義的方法`鍵:NilClass」

和如果我不具有@expt_samples,它給我一個錯誤「找不到id在示例」

有人可以建議如何更新從一個窗體的示例id集,還創建新的關聯到sample_ids的Expt模型記錄另一種形式。

所有的建議表示讚賞。

乾杯

回答

0

您需要參考Rails的accepts_nested_attributes_for

要了解如何將現有對象與形式的鏈接,你可以看到的例子在這裏:

http://josemota.net/2010/11/rails-3-has_many-through-checkboxes/

rails 3 has_many :through Form with checkboxes

has_many through checkboxes

+0

您好zsquare,我真正想要的是允許用戶通過複選框選擇他已經提交的示例。然後使用這些samples_ids,我想創建一個單獨的研究模型(字段),同時通過sample_fields更新樣本模型的某些屬性。 accept_nested_attributes_for允許我使用不需要的id創建新樣本,我希望它創建一個帶有字段的新研究模型,併爲我提供用戶選擇的sample_ids字段,以便我可以將新的研究模型與現有選擇相關聯樣本。我希望我清楚地描述它。 – A1aks

+0

@AkshayBhat檢查我更新的答案。 – zsquare

+0

你是一個救星zsquare。應用程序就像魅力。歡呼 – A1aks