2012-05-02 75 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