大家好我有关于params [:value]的问题。
我的模型必须由名为value的变量属性组成,我可以在控制器中使用此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有多少才能为用户生成一些东西。此外,我不确定宝石Bioruby是否可以在Rails中访问〜我不必担心RUby,但Rails架构很难。我在我的gemfile中定义了宝石Bioruby。希望它有效。
答案 0 :(得分:1)
params包含从提交的表单中收到的信息。所以,这实际上取决于你是否在表单中有一个名称为“value”的字段(编辑框,选择框等)。因为,大多数Rails表单都是从Model生成的,通常意味着你的模型中有一个属性“value”。
答案 1 :(得分:1)
如果您尝试使用参数创建/更新模型项,则会关注模型。您可以将任何内容作为参数传递给控制器,但如果您创建/更新模型项,则参数必须位于模型中。