ruby-on-rails - Rails 无法从表单向控制器传递/访问参数
问题描述
我正在关注本教程,并设置了我需要的内容,但是即使我的网络标头在发布时获得了参数,我似乎也无法获得参数。
我基本上是在尝试使用 axlsx gem 制作下载的 xlsx。在我的控制器中,这一行,我需要传递参数:
@records = Report.get_producer_clients(params[:producer_id])
下载excel文件效果很好,但我尝试@records
在第一行打印,但没有显示任何内容。
谁能告诉我我错过了什么?
这是我的代码片段: Report.rb (Model)
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"
路线.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
解决方案
你打电话Report.get_producer_clients(params[:producer_id])
,但没有params[:producer_id]
。您的表单发送到控制器params[:report][:producer_id]
。params
作为散列工作,而不是在孩子的散列中搜索键。
PS 另外,不要给带有前缀“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
推荐阅读
- python - python-在写入模式下打开快捷方式文件给我一个返回值 41
- php - 通过 ews 发送房间绑定事件
- notepad++ - 在 editplus 或 Notepad++ 中查找并替换匹配的前 100 行
- amazon-cognito - 如何在 AWS Cognito 中忽略 MFA 身份验证
- css - 嗨,我的媒体查询在生产中遇到问题,而它在本地主机上工作。(React、css、scss、样式化组件、heroku、Webpack)
- javascript - Django中的Ajax调用以查看用户是否已经存在
- nginx - 通过导入 videojs 和 videojs-record 导致 NGINX 内存泄漏
- python - 股票的移动平均线交叉
- python - Scrapy下载中间件返回response对象时不执行del操作
- r - 如何增加ggplot中类别之间的空间?