我已经看到在许多创建表单的例子中,如果你使用的是form_for,你通常会输入f.radion_button但是如果你使用的是form_tag,那么你使用tag,在我的情况下,我正在使用form对于。如果我使用f.radio_button,则没有值传递给我的对象变量。但是,如果我使用radio_button_tag,则值将传递到对象中。为什么会这样?
我的例子:(我使用了radio_button_tag并且随机方法正常工作.Ther est返回空bcoz它不是radio_button _tag)
<%= form_for(@generator, :url => user_generators_path(:user_id => current_user.id, :id => @generator.id)) 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>
<% else %>
<fieldset class ="primer">
<legend><strong>Method Use :</strong></legend><br>
<h3 align="left"><font size ="5"><b>Step 1: <u>Choose only ONE of the methods</u></font></b></h3>
<table class="p_gen">
<thead>
<tr>
<th class="method1"><label>Randomly</label><br />
<%= radio_button_tag(:choice, 'Randomly',checked:true )%>
<p> ( Generate a primer randomly )</p></th>
<th class="method2"><label>Specified ATGC</label><br />
<%= f.radio_button(:choice,'Specified ATGC')%>
<p> ( Generate a primer with number of A,T,G and C )</p></th>
<th class="method3"><label>Seating</label><br />
<%= f.radio_button(:choice,'Seating')%>
<p> ( Generate a primer according to your preference )</p></th>
</tr>
</thead>
<tbody>
<tr>
<td class="method1">
<p> Input length of the primer you want : </p>
<label>Primer Length :</label>
<%= f.number_field :primer_length , min: 6 , max: 35%>
</td>
<td class="method2">
<p>Input the number of each base the primer should have</p>
<label>Number of A :</label>
<%= f.number_field :no_A %><br />
<label>Number of T :</label>
<%= f.number_field :no_T %><br />
<label>Number of G :</label>
<%= f.number_field :no_G %><br />
<label>Number of C :</label>
<%= f.number_field :no_C %><br />
Total bases:<span></span>
</td>
<td class="method3">
<p> Input your preference sequence (only IUPAC nucleotide).</p>
<p><b><u>IUPAC Nucleotide :</u></b></p>
<p>A,T,G,C,R,Y,S,W,K,M,B,D,H,V,N </p>
Example: <br />
Preference primer = TAGGCT<b>N</b>TTA<b>N</b>GAC<b>N</b> <br />
N = Any base ( A/ T / G / C) <br /><br />
<label>Desired sequence :</label><br>
<%= f.text_field :user_seq,maxlength: 35%>
</td>
</tr>
</tbody>
</table>
</fieldset>
<br>
<fieldset class ="sample">
<h4><font size="5"><b>Step 2: <u>Choose 'Yes' if you want to input reference sequence for Binding-time analysis </font></u></b></h4>
<legend><strong>Do you have NCBI data to extract / FASTA file to input?</strong></legend><br>
<label>Yes</label>
<%= f.radio_button(:result_choice,'Yes')%>
<label>No</label>
<%= f.radio_button(:result_choice,'No')%>
<br>
</fieldset>
<br><div class = "button">
<%=f.submit("Generate", :class => "Gbutton_class") %>
</div>
<%end %>
<%end%>
</body>
</html>
控制器
def create
@generator = current_user.generators.build(params[:generator])
@generator.choice = params[:choice]
if params[:choice] == 'Randomly'
@generator.random_generate(generator_params)
elsif params[:choice] == 'Specified ATGC'
@generator.specified_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
elsif params[:choice] == 'Seating'
@generator.seating(params[:user_seq])
end
@generator.result_choice=params[:result_choice]
@generator.save
respond_to do |format|
if @generator.result_choice == 'Yes'
format.html { redirect_to(user_generator_path(:user_id => current_user.id, :id => generator.id)) }
else
format.html { redirect_to(user_generators_path(@generator) ) }
end
end
end