rails 6 中没有路由匹配多个路由参数
No Route Matching in rails 6 for multiple route parameters
routes.rb:
get '/get_text_by_tablenum/:filename_id/:tablenum_id', to: 'dashboard#get_text_by_tablenum'
ajax:
$.ajax({
dataType: "json",
cache: false,
url: '/get_text_by_tablenum/' + filename + '/' + id_value_string,
timeout: 5000,
error: function(XMLHttpRequest, errorTextStatus, error) {
alert("Failed to submit : " + errorTextStatus + " ;" + error);
},
success: function(data) {
console.log(data)
}
rake 路线:
GET /get_text_by_tablenum/:filename_id/:tablenum_id(.:format) dashboard#get_text_by_tablenum
错误信息:
No route matches [GET] "/get_text_by_tablenum/MPLX_1Q20_Conf_Call_Slides.pdf/12"
我收到此错误消息的原因是什么?
很简单,您的资源必须位于 URL 的末尾。
MPLX_1Q20_Conf_Call_Slides.pdf
它是一个文件而不是路径。
示例:
get "path/to/file.pdf", to: "my_controller#action"`
圆点已被删除,因为它用作格式化路由的分隔符。如果您需要在 :filename 中使用点,请添加约束。 constraints: { filename: /.*/ }
现在您的路线如下所示:
get 'files/:filename', to: "files#index", constraints: { filename: /.*/ }
然后在浏览器中:
http://localhost:3000/files/MPLX_1Q20_Conf_Call_Slides.pdf
files_controller.rb
app/controllers/files_controller.rb
1: class FilesController < ApplicationController
2: def index
3: byebug
=> 4: filename = params[:filename]
5: send_file("#{Rails.root}/private/#{filename}",
6: :filename => "#{filename}",
7: :type => "application/pdf", #for example if pdf
8: :disposition => 'inline')
9: end
10: end
(byebug) params
<ActionController::Parameters {"controller"=>"files", "action"=>"index", "filename"=>"MPLX_1Q20_Conf_Call_Slides.pdf"} permitted: false>
routes.rb:
get '/get_text_by_tablenum/:filename_id/:tablenum_id', to: 'dashboard#get_text_by_tablenum'
ajax:
$.ajax({
dataType: "json",
cache: false,
url: '/get_text_by_tablenum/' + filename + '/' + id_value_string,
timeout: 5000,
error: function(XMLHttpRequest, errorTextStatus, error) {
alert("Failed to submit : " + errorTextStatus + " ;" + error);
},
success: function(data) {
console.log(data)
}
rake 路线:
GET /get_text_by_tablenum/:filename_id/:tablenum_id(.:format) dashboard#get_text_by_tablenum
错误信息:
No route matches [GET] "/get_text_by_tablenum/MPLX_1Q20_Conf_Call_Slides.pdf/12"
我收到此错误消息的原因是什么?
很简单,您的资源必须位于 URL 的末尾。
MPLX_1Q20_Conf_Call_Slides.pdf
它是一个文件而不是路径。
示例:
get "path/to/file.pdf", to: "my_controller#action"`
圆点已被删除,因为它用作格式化路由的分隔符。如果您需要在 :filename 中使用点,请添加约束。 constraints: { filename: /.*/ }
现在您的路线如下所示:
get 'files/:filename', to: "files#index", constraints: { filename: /.*/ }
然后在浏览器中: http://localhost:3000/files/MPLX_1Q20_Conf_Call_Slides.pdf
files_controller.rb
app/controllers/files_controller.rb
1: class FilesController < ApplicationController
2: def index
3: byebug
=> 4: filename = params[:filename]
5: send_file("#{Rails.root}/private/#{filename}",
6: :filename => "#{filename}",
7: :type => "application/pdf", #for example if pdf
8: :disposition => 'inline')
9: end
10: end
(byebug) params
<ActionController::Parameters {"controller"=>"files", "action"=>"index", "filename"=>"MPLX_1Q20_Conf_Call_Slides.pdf"} permitted: false>