"croppable_ima-6ren">
gpt4 book ai didi

ruby-on-rails - 尽管有 Rails 路由输出结果,但 Rails 没有路由匹配

转载 作者:行者123 更新时间:2023-12-05 04:35:38 26 4
gpt4 key购买 nike

尽管在 rails routes 中找到了匹配的路由,但路由仍无法匹配

No route matches {:action=>"update", :controller=>"croppable_images", :name=>"cover_photo", :record_type=>"Stager"}, possible unmatched constraints: [:name] excluded from capture: No host specified, no public_key specified, no project_id specified

ActionController::UrlGenerationError (No route matches {:action=>"update", :controller=>"croppable_images", :name=>"cover_photo", :record_type=>"Stager"}, possible unmatched constraints: [:name]):

app/controllers/croppable_images_controller.rb:19:in `edit'
127.0.0.1 - - [04/Feb/2022:16:28:44 CST] "GET /stager/profile/cover_photo HTTP/1.1" 500 143893
http://localhost:3000/stager/profile -> /stager/profile/cover_photo

路由调用

@edit_image_url = stagers_stager_croppable_image_path('cover_photo')
<%=
form_for(
@croppable_image,
url: @edit_image_url,
method: :put
) do |f|
%>

routes.rb 部分:

namespace :stagers, path: 'stager' do
resource(
:stager,
path: 'profile',
only: %i[edit update],
path_names: {
edit: ''
}
) do
%w[
profile_photo
cover_photo
].each do |croppable_image_name|
resources(
:croppable_image,
controller: '/croppable_images',
path: '',
param: :name,
only: %i[edit update],
path_names: {
edit: ''
},
defaults: {
record_type: 'Stager'
},
constraints: {
name: croppable_image_name
}
)
end
end
end

路线:

<表类="s-表"><头>助手HTTP 动词路径 Controller #Action<正文>edit_stagers_stager_croppable_image_path获取/stager/profile/:名称(.:格式)可裁剪图像#edit {:record_type=>"Stager", :name=>"profile_photo"}stagers_stager_croppable_image_path补丁/stager/profile/:名称(.:格式)可裁剪图像#update {:record_type=>"Stager", :name=>"profile_photo"}放置/stager/profile/:名称(.:格式)可裁剪图像#update {:record_type=>"Stager", :name=>"profile_photo"}获取/stager/profile/:名称(.:格式)可裁剪图像#edit {:record_type=>"Stager", :name=>"cover_photo"}补丁/stager/profile/:名称(.:格式)可裁剪图像#update {:record_type=>"Stager", :name=>"cover_photo"}放置/stager/profile/:名称(.:格式)可裁剪图像#update {:record_type=>"Stager", :name=>"cover_photo"}

最佳答案

您正在设置带有约束的路线

%w[profile_photo cover_photo].each do |croppable_image_name|
resrources
...
constraints: { name: croppable_image_name }
end
end

因此您需要将 name: 添加到您的 route ,这样应该可以工作:

stagers_stager_croppable_image_path(name: 'profile_photo')

但是,由于您没有在每个循环中使用特定的路由名称,因此将名称设置为 cover_photo 的调用将不起作用。我想你可能想使用像这样的正则表达式

constraints: { name: /(cover|profile)_photo/ }

尝试像这样声明你的路线(抱歉我弄乱了你的风格)

Rails.application.routes.draw do
namespace :stagers, path: 'stager' do
resource(:stager, path: 'profile', only: %i[edit update], path_names: { edit: '' }) do
resources(
:croppable_image,
controller: '/croppable_images',
path: '',
param: :name,
only: %i[edit update],
path_names: {
edit: ''
},
defaults: {
record_type: 'Stager'
},
constraints: {
name: /(cover|profile)_photo/
}
)
end
end
end

关于ruby-on-rails - 尽管有 Rails 路由输出结果,但 Rails 没有路由匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70993560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com