【发布时间】:2022-11-26 20:49:10
【问题描述】:
我需要在按下按钮时显示通知/弹出窗口。类似的方法在应用程序的其他视图和控制器中也有效,但在此“导入”按钮上,很长时间以来都不起作用。 redirect_to 都不能在控制器中工作,而它们在其他控制器中的类似用法可以工作。
routes.rb:
Rails.application.routes.draw do
namespace :admin do
get '', to: 'dashboard#index', as: 'root'
# resourceful routes
resources :oauth_clients
resources :tenants do
resources :sites do
#resources :production_shifts
resources :units do
resources :log_data_fields, only: [:import, :create, :index, :destroy, :download_csv] do
get :download_csv
# collection route
collection do
post :import #post action
end
end
log_data_fields_controller.rb:
class Admin::LogDataFieldsController < Admin::BaseController
require 'csv'
# import request(this is gonna be a POST action)
def import
logger.debug("*****Testing the logger.*****")
file = params[:log_data_field][:file]
# return redirect_to [:admin, @tenant, @site, @unit], notice: "Only CSV please !!" unless file.content_type == "text/csv"
return redirect_to admin_tenant_site_unit_log_data_fields_url, notice: "Only CSV please !!" unless file.content_type == "text/csv"
file = File.open(file)
csv = CSV.parse(file, headers: true)
# csv = CSV.parse(file, headers: true, col_sep: ";")
@unit = Unit.find_by_id(params[:unit_id])
# p @unit.id
total_rows = CSV.read(file).count
count = 1
# binding.b
csv.each do |row|
tag_hash = {}
tag_hash[:name] = row["Name"]
tag_hash[:alias] = row["Alias"]
tag_hash[:column_type] = row["Type"]
tag_hash[:unit_id] = @unit.id
tag_hash[:is_active] = row["Active"]
# binding.b
# p row
logger.debug("+++++++++++Mapping++++++++++++++")
@log_data_field = LogDataField.create(tag_hash)
# binding.b
if @log_data_field.save
count += 1
logger.debug("--------Saves--------")
# return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit),
else
# return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit),
# render :_importtags
end
end
logger.debug("-------------Going down----------")
if count == total_rows && count > 1
logger.debug("-------------All succeeded----------")
redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit), flash: { :notice => "Success"}
# flash.notice = "Success : Tags imported from CSV !"
elsif total_rows == 0
logger.debug("-------------All zero----------")
flash.alert = "Import Failure : CSV cant be empty"
render :action => 'index', :notice => "Import Failure : CSV cant be empty."
else
logger.debug("-------------Failed down----------")
flash.alert = "Import Failure"
render :action => 'index', :notice => "Import Failure"
end
redirect_to import_admin_tenant_site_unit_log_data_fields_url(@tenant, @site, @unit), notice:"Imported tags !"
end
_importtags.html.haml:
%p{:style => "color: green"}= notice
= form_with model:@log_data_field, url: import_admin_tenant_site_unit_log_data_fields_path, method: :post do |form|
- if @log_data_field.errors.any?
#error_explanation
%h2= "#{pluralize(@log_data_field.errors.count, "error")} prohibited this log_data_field from being saved:"
%ul
- @log_data_field.errors.full_messages.each do |message|
%li= message
-# = link_to 'Download sample csv', [:admin, @tenant, @site, @unit, @log_data_field], method: :get
= form.file_field :file, accept: ".csv"
-# = form.file_field :file
<br>
<br>
-#button.btn.primary{:type => "submit", data: { disable_with: "Please wait..."}}
%button.btn.primary{:type => "submit"}
= "Import"
评论是我尝试过的东西。
抱歉,如果您发现问题或其结构非常不专业,但我是初学者并且经常学习。我需要在点击 Import 按钮时再次渲染视图,以显示任何错误(如果可用)或成功从 csv 导入标签。当非 csv 文档以应该发出警告但没有出现的形式提交时,还存在通知不可见或弹出和 redirect_to 不工作的问题。
我相信解决方案将非常简短,或者在理解路径与 url 路由时出现一些拼写错误或愚蠢的错误。
编辑根据@markets的建议,我用return做了所有的redirect_to,它们在两者之间使用,所以通知有效,但它们只在刷新时出现。单击按钮仍然无法立即获得它们:
class Admin::LogDataFieldsController < Admin::BaseController
before_action :set_tenant
before_action :set_site
before_action :set_unit
require 'csv'
# import request(this is gonna be a POST action)
def import
logger.debug("*****Testing the logger.*****")
file = params[:log_data_field][:file]
# return redirect_to [:admin, @tenant, @site, @unit], notice: "Only CSV please !!" unless file.content_type == "text/csv"
return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit), notice: "Only CSV please !!" unless file.content_type == "text/csv"
file = File.open(file)
csv = CSV.parse(file, headers: true)
# csv = CSV.parse(file, headers: true, col_sep: ";")
@unit = Unit.find_by_id(params[:unit_id])
# p @unit.id
total_rows = CSV.read(file).count
count = 1
# binding.b
csv.each do |row|
tag_hash = {}
tag_hash[:name] = row["Name"]
tag_hash[:alias] = row["Alias"]
tag_hash[:column_type] = row["Type"]
tag_hash[:unit_id] = @unit.id
tag_hash[:is_active] = row["Active"]
# binding.b
# p row
logger.debug("+++++++++++Mapping++++++++++++++")
@log_data_field = LogDataField.create(tag_hash)
# binding.b
if @log_data_field.save
count += 1
logger.debug("--------Saves--------")
# return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit),
# else
# return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit),
# render :_importtags
end
end
logger.debug("-------------Going down----------")
if count == total_rows && count > 1
logger.debug("-------------All succeeded----------")
return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit), flash: { :notice => "Success : Tags imported from CSV !"}
elsif total_rows == 0
logger.debug("-------------All zero----------")
return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit), flash: { :notice => "Import Failure : CSV cant be empty"}
else
logger.debug("-------------Failed down----------")
return redirect_to admin_tenant_site_unit_log_data_fields_path(@tenant, @site, @unit), flash: { :notice => "Import Failure : PLease check CSV"}
end
redirect_to import_admin_tenant_site_unit_log_data_fields_url(@tenant, @site, @unit), notice:"Imported tags !"
end
【问题讨论】:
-
应该是
return and redirect_to admin_tenant_site_unit_log_data_fields_url -
不@Vishal 它给出错误
void value expression return and redirect_to admin_tenant... ^~~~~~
标签: ruby-on-rails redirect ruby-on-rails-6 rails-routing