我正在遵循此tutorial,并设置了所需的内容,但是即使我的网络标头在发布时得到了参数,我似乎也无法获得参数。
我基本上是在尝试使用axlsx gem来下载xlsx。 在控制器的这一行中,我需要传递参数:
@records = Report.get_producer_clients(params[:producer_id])
下载excel文件效果很好,但是我尝试在第一行打印@records
,但没有显示任何内容。
谁能告诉我我错过了什么?
以下是我的代码段: Report.rb(模型)
def self.generate_client(id)
client = Array.new
client << get_cinfo(id) << get_caddress(id) << get_cpolicies(id) << get_ccontactinfo(id)
return client
end
def self.get_producer_clients(id)
clients = Array.new
Request.records(id).each do |client|
clients << generate_client(client)
end
return clients
end
Reports_Controller.rb
def generate_clients
@report = Report.new(report_params)
@id = params[:producer_id]
@report.queue_status = "Completed"
@report.created_by = current_user.id
respond_to do |format|
if @report.save
if @report.report_type == "Producer Clients"
@records = Report.get_producer_clients(params[:producer_id])
end
format.html {
if @report.batch_generate
outstrio = StringIO.new
@report.update_attribute(:date_start, DateTime.now)
p = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "reports/create_clients"
outstrio.write(p)
@report.update_attribute(:date_end, DateTime.now)
send_data outstrio.string, filename: "#{DateTime.now.to_date} #{@report.report_name}.xlsx"
end
}
format.json { render :show, status: :created, location: @report }
else
@report.errors.each do |err|
puts err.full_message
end
format.html { redirect_to :back, alert: 'Both dates must be filled up.' }
format.json { render json: @report.errors, status: :unprocessable_entity }
end
end
def report_params
params.require(:report).permit(:from_due_date, :to_due_date, :team_id, :report_type, :report_for, :survey_id, :individual_id, :team_id, :group_id, :user_id, :producer_id, :batch_generate, :queue_status)
end
_clients_form.html.haml
= simple_form_for @report , url:generate_clients_reports_path do |f|
= f.error_notification
.form-inputs
.form-group.col-md-12
= f.input :producer_selected, :url => autocomplete_producer_search_string_requests_path, :as => :autocomplete, :wrapper => :field_multi8, :label_html => { :class => "col-md-2 text-right" }, :input_html => { :class => "form-control" }, :id_element => "#report_producer_id", :label => "Producer :", :placeholder => "Find Producer", :update_elements => {}, :autofocus => true
= f.input_field :producer_id, :as => :hidden
.form-actions
.form-group.col-sm-12{style: "margin-top:20px"}
.col-sm-9
.col-sm-2
= f.submit "Generate", class: "btn btn-new btn-block"
routes.rb
resources :reports do
collection do
post :generate_clients
get :clients_list
get :clients_new
end
member do
get :download
patch :on_hold
patch :activate
patch :cancel
end
end
答案 0 :(得分:0)
您致电Report.get_producer_clients(params[:producer_id])
,但没有致电params[:producer_id]
。您的表单发送到控制器params[:report][:producer_id]
。 params
用作哈希,而不是在儿童哈希中搜索密钥。
P.S。另外,不要给带有前缀“ get_”的方法命名。这在社区中是不被接受的。您无需在每个方法的最后一行写return
,因此ruby总是返回该行。首选使用长度小于10行的方法,其他方法重写为两个或多个方法。尝试Rubocop或类似的宝石。因此,您的模型方法如下所示:
def self.generate_client(id)
[cinfo(id), caddress(id), cpolicies(id), ccontactinfo(id)] #use Hash instead Array
end
def self.producer_clients(id)
Request.records(id).each_with_object([]) do |client, clients|
clients << generate_client(client)
end
end
答案 1 :(得分:0)
在您的控制器中,您不应该执行Report.get_producer_clients(report_params[:producer_id])
吗?
编辑:
请参阅有关强参数的文档:
http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters