我传入:number作为参数,然后将其分配给控制器中的变量@test_suite_num before_filter
。Ruby On Rails:使用创建控制器函数中新控制器函数的参数
在我的控制器中的new
函数中,我使用变量@test_suite_num过滤名为Test
的表以获取其ID与@test_suite_num相匹配的条目数。我使用此计数来生成适当数量的子资源test_run
。
我对create
做了同样的事情,但由于某些原因,只要我在依赖@test_suite_num
的表单中使用这些变量,就会得到零错误。我假设由于创建永远不会接受一个参数,变量永远不会被初始化。我如何在create
函数中使用@test_suite_num?
控制器代码:
before_filter :get_number, :only => [:new, :create]
def get_number
@test_suite_num = params[:number]
end
新
def new
@test_suite_run = TestSuiteRun.new
@tests = Test.find(:all, :conditions => { :test_suite_id => @test_suite_num })
@tests.count.times do test_run = @test_suite_run.test_runs.build end
end
创建
def create
@test_suite_run = TestSuiteRun.new(params[:test_suite_run])
@tests = Test.find(:all, :conditions => { :test_suite_id => @test_suite_num })
@tests.count.times do test_run = @test_suite_run.test_runs.build(params[:test_suite_run]) end
if @test_suite_run.save
flash[:success] = "Run Added Succesfully"
redirect_to test_suite_runs_path(@test_suite_run)
else
render 'new'
end
end
编辑:
我只是用回报率,所以我的风格是非常的混乱开始。但是我的问题依然存在。我的问题是,在我的形式,我使用@tests,这与我通过在参数初始化我可以查看的形式就好了,但是当我点击提交,我得到:
Showing /Users/vsp/Documents/rails_projects/web_db/app/views/test_suite_runs/_form.html.erb where line #13 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Extracted source (around line #13):
10:
11: <%= f.fields_for :test_runs do |builder| %>
12:
13: Test <%= @tests[@i].id %><br><br>
14:
15: <%= render "partial", :f => builder %>
16: <hr>
显然,这手段创建控制器没有得到参数[:数字],因此表单提交休息。我的创建函数有什么问题吗?
PARAMS:{ “UTF8”=> “✓”, “authenticity_token”=> “/ vbOcFozTEAvoa03OUBfyxJbJ9AQp8m8yA04LkxlRE8 =”, “test_suite_run”=> { “test_runs_attributes”=> { “0”=> {“为test_id “=>”1“,”machine_id“=>”“,”date“=>”2012-07-09 15:59:53 -0700“,”status“=>”“,”result“=>”“ },“1”=> {“test_id”=>“3”,“machine_id”=>“”,“date”=>“2012-07-09 15:59:53 -0700”,“status”=> “”,“result”=>“”},“2”=> {“test_id”=>“6”,“machine_id”=>“”,“date”=>“2012-07-09 15:59: 53 -0700“,”status“=>”“,”result“=>”“},”3“=> {”test_id“=>”7“,”machine_id“=>”“,”date“=> “2012-07-09 15:59:53 -0700”,“status”=>“”,“result”=>“”}},“date”=>“2012-07-09 15:59:53 - 0700“},”commit“=>”Submit“,”action“=>”create“,”controller“=>”test_suite_runs“}
通过此查看,它只是我窗体中的所有text_input字段。
你可以做一个'把params.to_s'在顶部用创造行动?该视图仍然失败,但服务器将在服务器日志中显示参数 – varatis 2012-07-09 22:50:17