ruby-on-rails-4 相关问题

针对Rails第4版特有的问题。如果您的问题通常适用于Ruby on Rails,请使用标记[ruby-on-rails]。

Rails 结合操作流中的 pdf

我的控制器 #applications 中有一个操作,可以呈现我的网站应用程序页面的 html 页面。当我使用 :pdf 格式指定此操作的路径时,它会将页面呈现为 p...

回答 2 投票 0

为什么最好将电话号码存储为字符串而不是整数?

正如问题所述,为什么将电话号码存储为字符串而不是整数在 Telephone_number 列中被认为是最佳实践? 不确定我是否理解这样做的理由。 ...

回答 6 投票 0

如何在 OSX 上安装 `libgmp3-dev`?

我安装1.8.3版本的json gem时遇到错误: dyld:惰性符号绑定失败:找不到符号:_rb_str_new_static 引用自:/Users/gnerkus/.rvm/gems/ruby-...

回答 2 投票 0

液体过滤器不起作用(rails 应用程序)

我使用 Liquid 作为价格标签的模板语言,用户可以从我的网站下载这些标签。 液体单独工作效果很好,但自定义过滤器不起作用。他们只是什么都不做。 这是一个

回答 2 投票 0

有办法写得干净吗?

所以我有一个标志来禁用和启用某个功能。并将根据它必须切换标志的变量 flag_a、flag_b是我的两个功能标志,变量输入字符串可以是none、all、f...

回答 1 投票 0

记录不存在则创建

我的 Rails 应用程序中有 3 个模型 班级联系方式 < ActiveRecord::Base belongs_to :survey, counter_cache: :contact_count belongs_to :voter has_many :contact_attempts end class Survey <

回答 5 投票 0

如何在 ruby 中反转顺序

如何用 Ruby 编写一个代码片段,以相反的顺序打印出 1 到 200 之间的每个 3 的倍数? 这是我到目前为止的代码: (1..200).每个做 |我| ##然后循环它 如果我%3...

回答 7 投票 0

Ruby 版本管理器

我是 Ruby 新手,目前使用 Windows 10。我在安装 RVM 时遇到困难。由于我从事具有不同 Ruby 版本的不同项目,因此我想使用 RVM 来轻松管理它们。目前...

回答 1 投票 0

Stripe - 取消订阅

更新:问题在于,当创建新客户时,Stripe 会使用 card_token 而不是客户 ID 进行响应。也许我做错了什么。 我已经在 Rails 应用程序上设置了 Stripe...

回答 1 投票 0

Ruby on Rails 如何将 javascript 放入 head 标签中?

我正在使用 ruby 2/rails 4 并尝试将我的 javascript 放入 但所有 javascript 都会在页面末尾, 我正在尝试使用此代码但不起作用: 在应用程序/视图/布局/index.h...

回答 2 投票 0

在 Rails 中,我可以通过委托方法订购查询吗?

我在通过委托方法订购查询时遇到困难。我的任务是帮助将一个相当大的 Rails 3 应用程序升级到 Rails 4。我在索引操作中遇到了这个查询。 #

回答 1 投票 0

Rails Asset Pipeline - 如何确定资产是否存在?

有什么方法可以询问Asset Pipeline某个资产是否存在?我看到的建议主要涉及使用 File.exist? 手动检查文件路径。问题是我有几个g...

回答 4 投票 0

使代码看起来更好的替代方法或设计

我有一个班级人员的列被禁用(如果该人被禁用) 如果此人是残疾人,我们标记为 true 如果那个残疾人有轮椅,我们会将残疾人设为 false 但当我尝试获取...

回答 1 投票 0

资产编译在服务器环境中崩溃,并显示:“NoMethodError: undefined method `[]' for nil:NilClass”

为了这个我已经抓狂了。我一直与资产管道关系不好,它总是给我带来麻烦..今天又来了.. 当我尝试在本地编译我的资产时,

回答 4 投票 0

Ransack::Search 的未定义方法“name_cont”

我正在尝试使用 ransank gem。但由于某种原因我得到 Ransack::Search>:Ransack::Search 的未定义方法 `name_cont' 用户控制器.rb 定义索引 @q = User.ransack(params[:q]...

回答 2 投票 0

Heroku 上的 Rails 4 应用程序出现随机 ActiveRecord::StatementInvalid 错误

我的 Rails 4 应用程序在本地运行良好,而且当它以生产模式部署到 Heroku 时......直到我从第二个客户端访问该应用程序......然后单击几下后我开始得到以下信息......

回答 2 投票 0

在可安装rails引擎内运行ActiveAdmin时出错:无法加载此类文件--web_app/admin

我正在将 Rails 应用程序从单个应用程序转换为多个引擎。 我正在尝试将 ActiveAdmin 移动到运行前端应用程序的引擎中。 堆栈是: 红宝石 4.2.2 铁路...

回答 3 投票 0

如何在 Rails 中显示 SVG 图像?

我在 /app/assets/images/symbols.svg 上有一个 svg 图像,以此作为内容。 我在 /app/assets/images/symbols.svg 上有一个 svg 图像,以此作为内容。 <svg xmlns="http://www.w3.org/2000/svg"><symbol id="icon-search" viewBox="0 0 18 18"><path fill="currentColor" d="M12.8 11.4c.9-1.2 1.4-2.7 1.4-4.3C14.2 3.2 11 0 7.1 0S0 3.2 0 7.1c0 3.9 3.2 7.1 7.1 7.1 1.6 0 3.1-.5 4.3-1.4l5.2 5.2 1.4-1.4-5.2-5.2zM2 7.1C2 4.3 4.3 2 7.1 2s5.1 2.3 5.1 5.1-2.3 5.1-5.1 5.1S2 9.9 2 7.1z"/></symbol><symbol id="icon-view-default" viewBox="0 0 18 18"><g fill="currentColor"><path d="M8 11H0V0h8v11zM18 5h-8V0h8v5zM8 18H0v-5h8v5zM18 18h-8V7h8v11z"/></g></symbol><symbol id="icon-view-alt" viewBox="0 0 18 18"><g fill="currentColor"><path d="M8 8H0V0h8v8zM18 8h-8V0h8v8zM8 18H0v-8h8v8zM18 18h-8v-8h8v8z"/></g></symbol><symbol id="icon-view-full" viewBox="0 0 18 18"><g fill="currentColor"><path d="M18 8H0V0h18v8zM18 18H0v-8h18v8z"/></g></symbol><symbol id="icon-facebook" viewBox="0 0 9 19"><path fill="currentColor" d="M6.7 3.6H9V0H6.3c-3.3.1-4 2.1-4 4.1V6H0v3.5h2.2V19h3.4V9.5h2.8L8.9 6H5.6V4.9c0-.7.4-1.3 1.1-1.3z"/></symbol><symbol id="icon-twitter" viewBox="0 0 17.9 15"><path fill="currentColor" d="M17.9 1.8c-.3.2-1.2.5-2 .6.5-.3 1.3-1.3 1.5-2.1-.5.3-1.7.8-2.3.8C14.4.5 13.5 0 12.4 0c-2 0-3.7 1.7-3.7 3.8 0 .3 0 .6.1.8-2.8-.1-6-1.5-7.8-4-1.1 2-.2 4.2 1.1 5-.4 0-1.2-.1-1.6-.4 0 1.3.6 3.1 2.9 3.7-.5.4-1.3.3-1.6.3.1 1.1 1.6 2.6 3.3 2.6-.6.7-2.6 2-5.1 1.6 1.7 1 3.7 1.6 5.8 1.6 6 0 10.6-5 10.3-11.1v-.1c.6-.4 1.3-1.1 1.8-2z"/></symbol><symbol id="icon-pinterest" viewBox="0 0 18 18"><path fill="currentColor" d="M2.3 0h13.4C17 0 18 .9 18 2.3v13.4c0 1.4-1 2.3-2.3 2.3H2.3C1 18 0 17.1 0 15.7V2.3C0 .9 1 0 2.3 0zm10.8 2c-.4 0-.8.4-.8.8v1.9c0 .4.4.8.8.8h2c.4 0 .8-.4.8-.8V2.8c.1-.4-.3-.8-.8-.8h-2zM16 7.6h-1.6c.2.5.2 1 .2 1.5 0 3-2.5 5.4-5.6 5.4-3.1 0-5.6-2.4-5.6-5.4 0-.5.1-1.1.2-1.5H2v7.6c0 .4.3.7.7.7h12.5c.4 0 .7-.3.7-.7l.1-7.6zM9 5.5C7 5.5 5.4 7 5.4 9c0 1.9 1.6 3.5 3.6 3.5s3.6-1.6 3.6-3.5c0-2-1.6-3.5-3.6-3.5z"/></symbol><symbol id="icon-tumblr" viewBox="0 0 11 19"><path fill="currentColor" d="M8.6 15.9c-1.6 0-2.3-1.3-2.3-2.2V7.8h4.1v-3h-4V.1S4.7 0 3.5 0C3.5 4.1 0 4.9 0 4.9v2.8h2.3v7C2.3 17.1 4.4 19 7 19s4-1.1 4-1.1v-3.1c0 .1-.8 1.1-2.4 1.1z"/></symbol><symbol id="icon-menu" viewBox="0 0 274.5 224.5"><g fill="currentColor"><path d="M274 53.8H.5V.5H274v53.3zM274 138.9H.5V85.6H274v53.3zM274 224H.5v-53.3H274V224z"/></g></symbol><symbol id="bar" viewBox="0 0 274.5 54.3"><path fill="currentColor" d="M274 53.8H.5V.5H274v53.3z"/></symbol><symbol id="arrow" viewBox="0 0 38 14"><path fill="currentColor" d="M31 0l-1.4 1.4L34.2 6H0v2h34.2l-4.6 4.6L31 14l7-7-7-7z"/></symbol><symbol id="refresh" viewBox="0 0 19 16"><path fill="currentColor" d="M9.4 6.2l5.9 4.4L19 4.4l-1.7-1L15.7 6c-.9-3.5-4-6-7.7-6-4.4 0-8 3.6-8 8s3.6 8 8 8c2.1 0 4.1-.8 5.6-2.3l-1.4-1.4C11.1 13.3 9.6 14 8 14c-3.3 0-6-2.7-6-6s2.7-6 6-6c3 0 5.4 2.2 5.9 5l-3.3-2.4-1.2 1.6z"/></symbol></svg> 现在在我在网上找到的 html 模板上,图像显示为: <svg> <use xlink:href="assets/img/symbols.svg#bar"></use> </svg> 我怎样才能在轨道上完成这项工作。 我试过了: <use xlink:href="images/symbols.svg#bar"></use> 和 svg-inline gem 但在我的网页上都看不到图像。 任何人都可以帮我弄清楚如何进行这项工作吗? 在 Rails 应用程序中显示 SVG(可缩放矢量图形) 在资源编译中特别包含SVG文件类型 production.rb config.assets.precompile += %w( '.svg' ) # Must include to get inline SVGs to work in deploy config.assets.css_compressor = :sass 创建一个助手来显示 SVG myhelper.rb def show_svg(path) File.open("app/assets/images/#{path}", "rb") do |file| raw file.read end end 在您的视图中调用 SVG 处理助手 <%= show_svg('symbols.svg') %> 从你的问题来看,我并不是 100% 清楚你的实现,但这些步骤应该能让你看到你的 SVG 图像。一旦 SVG 可见,您就可以应用 CSS。 尝试使用 inline_svg gem,它添加了一个辅助方法,用于将文件中的 SVG 直接内联到模板中 您可以在 Rails 中插入 svg 内联,无需任何额外的 gem,使用如下所示: <%= render inline: Rails.root.join('public/icon.svg').read %> 如果您需要单击 svg 来执行某些操作,例如发出 POST 请求,请将整个内容包装在带有 link_to 的 do 中,如下所示: <%= link_to some_path(@something), method: :post do %> <%= render inline: Rails.root.join('public/icon.svg').read %> <% end %> 您可以像显示图像一样显示 SVG。 例如在haml中: %img{:src => "/images/image1.svg"} 希望有帮助! 我一直在尝试不同的解决方案,最终决定将 svg 文档放入 html.erb 部分。由于 svg 是有效的 html,因此将其从资产移动到视图就可以正常工作。 <%# app/views/home/index.html.erb %> <%= render partial: "svgs/some_svg.html.erb" 给定 app | views | | home | | | index.html.erb | | svgs | | | some_svg.html.erb 与nvm的方法类似,我有一个助手来帮助构建use标签的资源路径,并确保父svg标签具有适当的属性 def external_svg(identifier, attributes = {}) # identifier: <file name without extension>#<fragment id> file_name, fragment = identifier.split('#') file_name += '.svg' attributes.merge!(xmlns: 'http://www.w3.org/2000/svg') content_tag :svg, attributes do tag.use href: "#{image_path(file_name)}##{fragment}" end end 在视图中,您的 SVG 资源将被称为:external_svg 'symbols#icon-search', class: 'icon',生成的 html: <svg xmlns="http://www.w3.org/2000/svg" class="icon"> <use href="<asset pipeline path>/symbols.svg#icon-search" /> </svg> 由于外部引用的 SVG 现在得到了良好的支持,因此您不需要任何 SVG 特定的 gem 即可实现此功能。 使用这种方法时值得注意的优点(缓存)和缺点(CSS 样式限制)。请参阅此 Twitter 帖子 对于ActiveStorage,根据Elvn的回复,我发现有更好的方法 如果您有一个带有 item 的 has_one_attached :logo 物体: <%= show_svg(item.logo_blob) %> 那么助手就是: def show_svg(blob) blob.open do |file| raw file.read end end 很简单: 首先将svg添加到assets.rb中的资源预编译列表中 application.config.assets.precompile += %w(<your other files> symbol-defs.svg) 然后在 erb/haml 文件中引用它,如下所示: <svg class="icon gr-menu"> <use xlink:href="<%= asset_path('symbol-defs') %>#gr-menu"></use> </svg> 创建一个助手:(每种情况的内容都不同,自定义您自己的) def icon(icon, css_class: "") content_tag(:svg, class: "icon icon_#{icon} #{css_class}") do content_tag(:use, nil, 'xlink:href' => "#icon_#{icon}") end end 像这样使用它: <%= icon 'arrow-menu' , css_class: 'arrow-breadcrumb' %> 如果你可以使用 inline_svg gem。它很旧但仍然有效的解决方案维护得很好 如果您不能(例如公司政策),简而言之,这就是 gem 的作用: # app/helpers/svg_helper.rb module SvgHelper SVGFileNotFoundError = Class.new(StandardError) def inline_svg_tag(path, options = {}) path = Rails.root.join("app/assets/images/#{path}.svg") File.exist?(path) || raise(SVGFileNotFoundError, "SVG icon file does not exist: #{path}") svg_file_content = File.binread(path) if options.any? doc = Nokogiri::XML::Document.parse(svg_file_content) svg = doc.at_css("svg") svg["height"] = options[:height] if options[:height] svg["width"] = options[:width] if options[:width] svg["class"] = options[:class] if options[:class] svg_file_content = doc.to_html.strip end raw svg_file_content end end 测试: # spec/helpers/svg_helpers_spec.rb require "rails_helper" RSpec.describe SvgHelper do describe "#inline_svg_tag" do it "raises an error when the file does not exist" do expect { helper.inline_svg_tag("does-not-exist") }.to raise_error(SvgHelper::SVGFileNotFoundError) end it "when no options passed returns the SVG file contents with original HTML attribute values" do result = helper.inline_svg_tag("icons/profile") expect(result).to include("<svg") expect(result).to include('height="20"') expect(result).to include('width="20"') expect(result).not_to include("class") end it "when class option passed returns the SVG file contents with class HTML attribute" do result = helper.inline_svg_tag("icons/profile", class: "whatever") expect(result).to include("<svg") expect(result).to include('class="whatever"') end it "when height passed returns the SVG file contents with new height" do result = helper.inline_svg_tag("icons/profile", height: "12345") expect(result).to include("<svg") expect(result).not_to include('height="20"') expect(result).to include('height="12345"') end it "when width passed returns the SVG file contents with new width" do result = helper.inline_svg_tag("icons/profile", width: "54321") expect(result).to include("<svg") expect(result).not_to include('width="20"') expect(result).to include('width="54321"') end end end 我真的不知道如何解析SVG文件;但同样的事情也可以用于 SVG 文件。然后,您可以在 SVG 文件中使用 asset_path 助手;但问题是 Rails 默认解析 CSS、JS 和 ERB。这样,SVG 内部的链接就可以与资源管道配合使用。也许如果您尝试将名称从“symbols.svg”更改为“symbols.svg.erb”,它会解析文件以获取正确的网址。 你的问题是资产管道没有按照rails使用。 这就是解析(预处理)svg 文件的方法: 编译文件的默认匹配器包括application.js, application.css 和所有非 JS/CSS 文件(这将包括所有图像 自动资产)从应用程序/资产文件夹,包括你的宝石:[ Proc.new { |文件名,路径|路径 =~ /app/assets/ && !%w(.js .css).include?(File.extname(文件名)) }, /application.(css|js)$/ ] 有关助手方法的更多信息 2.3.2 CSS 和 Sass 使用资产管道时,必须重写资产路径并 sass-rails 提供 -url 和 -path 帮助器(在 Sass 中连字符, 在 Ruby 中下划线)用于以下资产类别:图像、字体、 视频、音频、JavaScript 和样式表。 image-url("rails.png") becomes url(/assets/rails.png) image-path("rails.png") becomes "/assets/rails.png". 也可以使用更通用的形式: asset-url("rails.png") becomes url(/assets/rails.png) asset-path("rails.png") becomes "/assets/rails.png" 2.3.3 JavaScript/CoffeeScript 和 ERB 如果您向 JavaScript 资产添加 erb 扩展,使其成为某种东西 例如 application.js.erb,然后您可以在中使用 asset_path 帮助器 你的 JavaScript 代码: $('#logo').attr({ src: "<%= asset_path('logo.png') %>" }); 这将写入所引用的特定资产的路径。 同样,您可以在 CoffeeScript 文件中使用 asset_path 帮助器 带有 erb 扩展名(例如 application.js.coffee.erb):$('#logo').attr 来源:“<%= asset_path('logo.png') %>” 您可以检查: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

回答 11 投票 0

ActiveAdmin 隐藏按条件删除操作

我有一些问题。 在 ActiveAdmin 中,我需要按条件隐藏 DELETE 操作。 我是为#index 页面做的。但我不知道如何用#show page 来实现这个技巧。 这是代码: 索引做 selectable_co...

回答 3 投票 0

heroku 部署上未显示输入类型复选框

我在 Heroku 上部署时遇到问题。我正在使用 Rails 4 和 postgresql。 以下代码在我的本地版本中运行良好,但在部署的版本中根本没有出现。所有其他

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.