Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails字体CORS策略_Ruby On Rails_Cors_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails Rails字体CORS策略

Ruby on rails Rails字体CORS策略,ruby-on-rails,cors,ruby-on-rails-5,Ruby On Rails,Cors,Ruby On Rails 5,我无法为CORS策略加载此字体 文件夹:app/assets/font/Inter UI.var.woff2 <%=preload_link_tag("Inter-UI.var.woff2", as:'font', crossorigin: "anonymous")%> 应用程序.rb Rails.application.configure do config.middleware.insert_before 0, Rack::Cors do allow do

我无法为CORS策略加载此字体

文件夹
app/assets/font/Inter UI.var.woff2

<%=preload_link_tag("Inter-UI.var.woff2", as:'font', crossorigin: "anonymous")%>
应用程序.rb

Rails.application.configure do

  config.middleware.insert_before 0, Rack::Cors do
    allow do
      origins '*'
      resource '*', :headers => :any, :methods => :any
    end
  end
 config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2)$/
Rails.application.config.assets.paths << Rails.root.join("app", "assets", "fonts")

尝试将此添加到
application.rb

Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2)$/

你可以简单地使用

<%= preload_link_tag("Inter-UI.var.woff2") %>



@font-face {
  font-family: 'Inter UI';
  font-style: italic;
  font-weight: 400;
  font-display: swap;
  unicode-range: U+000-5FF;
  src: font_url("Inter-UI.var.woff2") format("woff2-variations"), 
       font_url("Inter-UI-Italic.woff2") format("woff2"), 
       font_url("Inter-UI-Italic.woff") format("woff"); 
 }

还有do
rake资产:预编译

响应的HTTP状态代码是什么?您可以使用浏览器devtools中的网络窗格进行检查。是4xx还是5xx错误,而不是200 OK成功响应?如果粘贴
http://localhost:3000/assets/Inter-UI.var-e2e323d19d24946c4d481135af27ba00f3266aa9d4abe4262e97088feccb6ca4.woff2
插入浏览器地址栏,并尝试直接在浏览器中打开它?它只会显示“(失败)net::ERR_failed”(添加屏幕截图)。如果我直接转到字体文件URL,它会成功下载。您是否使用
0.0.0.0
以任何形式在浏览器中打开站点或启动Rails服务器?我不知道我是否理解了您的意思。我使用“rails s”启动服务器,它给我地址0.0.0.0:3000。每个url都是由您运行的railscan生成的
rails服务器--绑定本地主机--端口3000
?谢谢。它已经存在了。即使结尾有“woff2”,好吧,我再也不会有错误了。但没有被CSSDone加载,但没有任何更改。没有显示错误,字体不显示。我不知道。这是因为字体位置不正确。你会用新的测试吗
Rails.application.configure do

  config.middleware.insert_before 0, Rack::Cors do
    allow do
      origins '*'
      resource '*', :headers => :any, :methods => :any
    end
  end
end
Rails.application.configure do

  config.middleware.insert_before 0, Rack::Cors do
    allow do
      origins '*'
      resource '*', :headers => :any, :methods => :any
    end
  end

  allow do
    origins "*"
    resource "/assets/*", methods: :get,  headers: :any
   end
end
<%= preload_link_tag("Inter-UI.var.woff2") %>



@font-face {
  font-family: 'Inter UI';
  font-style: italic;
  font-weight: 400;
  font-display: swap;
  unicode-range: U+000-5FF;
  src: font_url("Inter-UI.var.woff2") format("woff2-variations"), 
       font_url("Inter-UI-Italic.woff2") format("woff2"), 
       font_url("Inter-UI-Italic.woff") format("woff"); 
 }
@font-face {
      font-family: 'Inter UI';
      font-style: italic;
      font-weight: 400;
      font-display: swap;
      unicode-range: U+000-5FF;
      src: url(<%= asset_path "Inter-UI.var.woff2" %>) format("woff2-variations"), 
           url(<%= asset_path "Inter-UI-Italic.woff2" %>) format("woff2"), 
           url(<%= asset_path "Inter-UI-Italic.woff" %>) format("woff"); 
     }