使用content_tag时,Ruby on Rails中的html表奇怪呈现(Odd rendering of an html table in Ruby on Rails when using content_tag)
我正在尝试使用content_tag方法在Ruby on Rails中构建一个表。
当我运行这个:
def itemSemanticDifferential(method, options = {}) if options[:surveyQuestion].any? then @template.content_tag(:tr) do @template.content_tag(:td, options[:surveyQuestion], :colspan => 3) end end @template.content_tag(:tr) do @template.content_tag(:th, options[:argument0]) end end
只渲染第二部分:
@template.content_tag(:tr) do @template.content_tag(:th, options[:argument0]) end
谁能告诉我为什么会这样?
I'm trying to build a table in Ruby on Rails with the help of the content_tag method.
When I run this:
def itemSemanticDifferential(method, options = {}) if options[:surveyQuestion].any? then @template.content_tag(:tr) do @template.content_tag(:td, options[:surveyQuestion], :colspan => 3) end end @template.content_tag(:tr) do @template.content_tag(:th, options[:argument0]) end end
Only the second part gets rendered:
@template.content_tag(:tr) do @template.content_tag(:th, options[:argument0]) end
Can anyone tell me why this is?
最满意答案
如果没有显式调用返回,Ruby Rails将返回它使用的最后一个变量。 (例如:)
def some_method(*args) a = 12 b = "Testing a String" # ... 3 end # This method will return the Integer 3 because it was the last "thing" used in the method
使用数组返回所有content_tag( 警告 :此方法将返回一个数组,而不是您期望的content_tag,您需要循环它):
def itemSemanticDifferential(method, options = {}) results = [] if options[:surveyQuestion].any? then results << @template.content_tag(:tr) do @template.content_tag(:td, options[:surveyQuestion], :colspan => 3) end end results << @template.content_tag(:tr) do @template.content_tag(:th, options[:argument0]) end return results # you don't actually need the return word here, but it makes it more readable end
正如问题的作者所说,你需要循环结果,因为它是一个content_tags数组。 此外,您需要使用
.html_safe
将content_tags输出为HTML(而不是字符串)。<% f.itemSemanticDifferential(:method, options = {}).each do |row| %> <%= row.html_safe %> <% end %>
Ruby Rails returns the last variable it used if no return is explicitly called. ( example: )
def some_method(*args) a = 12 b = "Testing a String" # ... 3 end # This method will return the Integer 3 because it was the last "thing" used in the method
Use an Array to return all the content_tag (WARNING: this method will return an Array, not a content_tag as you expect, you'll need to loop on it):
def itemSemanticDifferential(method, options = {}) results = [] if options[:surveyQuestion].any? then results << @template.content_tag(:tr) do @template.content_tag(:td, options[:surveyQuestion], :colspan => 3) end end results << @template.content_tag(:tr) do @template.content_tag(:th, options[:argument0]) end return results # you don't actually need the return word here, but it makes it more readable end
As asked by author of the Question, You need to loop on the result because it is an array of content_tags. Also, you need to use
.html_safe
to output the content_tags as HTML (not strings).<% f.itemSemanticDifferential(:method, options = {}).each do |row| %> <%= row.html_safe %> <% end %>
相关问答
更多-
为什么我应该使用content_tag而不是“常规”html?(why should I use content_tag instead of “regular” html?)[2022-04-22]
能够使用content_tag有助于在Ruby代码中编程生成HTML,例如:助手和演示者。 在我看来,我通常不会在项目的布局和模板中使用content_tags - 我认为它不会帮助编码的可读性。 在这里,我看不到任何生产力或表现的收益。 但是:建议的意思:如果这就是你的团队已经标准化的东西 - 与团队一起去。 Being able to use the content_tag helps when you are programmatically generating HTML inside of rub ... -
最好的方式使用html5数据属性与rails的content_tag帮助?(Best way to use html5 data attributes with rails content_tag helper?)[2022-10-28]
Rails 3.1附带内置助手: http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag 例如, tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)}) # =>这是一个奇怪的content_tag怪癖,但是如果你将它们嵌套在一起,你需要在内部标签的每个返回中使用一个concat 。 否则,你只需得到最后一个返回的字符串,而内层标签就会消失在以太网中。 可悲的是,根据我的经验,我发现复杂的嵌套不值得进入辅助方法。 也许,更好的方法是用装饰器模式,轨道部分或使用诸如单元宝石之类的东西来干掉html。 It's a weird quirk of content_tag but if you nest them you need to use a concat on ea ...此方法可以像这样创建HTML:first day second day .....................Seventh day 使用content_tag帮助程序进行复杂嵌套并不理想。 嵌套同一HTML标记的多个实例时,只会渲染最后一个标记,因此需要连接。 如果您像这样重构,那么您的thead将按预期显示: content_tag(:thead) do headlabel = ["Header1","Header2"] content_tag(:tr) do headlabel.each do |hlabel| concat(content_tag(:th, hlabel)) ...Ruby / Rails content_tag无法正确呈现(ruby 1.9.2)(Ruby/Rails content_tag isn't rendering correctly (ruby 1.9.2))[2021-12-11]
你可以像这样解决它: def menu_builder(page) items = [ "home", "faq", "store" ] content = "" items.each do |i| content << content_tag(:a, "#{i.capitalize}", :href => "/#{i}" ) end content.html_safe end 或者像这样修改模板: <%= raw menu_builder %> 使用任何。 You can ...如果没有显式调用返回,Ruby Rails将返回它使用的最后一个变量。 (例如:) def some_method(*args) a = 12 b = "Testing a String" # ... 3 end # This method will return the Integer 3 because it was the last "thing" used in the method 使用数组返回所有content_tag( 警告 :此方法将返回一个数组,而不是您期望的conten ...请更改为 content_tag(:span, options[:true_text], class: "status true") 请在使用方法之前阅读文档。 这是content_tag的文档 Please change this to content_tag(:span, options[:true_text], class: "status true") please read the docs before using a method. Here is docs for content_tag ...你不需要使用concat 。 每个Rails助手都返回一个字符串,其中包含一些html: tag(:br) # "
" 所以你最简单的帮手方法是: # "
" def br tag(:br) end 如果你有多个html的字符串,总结一下: # "" def modal_buttons content_tag(:button, "Close") + content_tag(:button, "Sav ...如何从Rails content_tag帮助函数返回原始HTML?(How to return raw HTML from Rails content_tag helper function?)[2023-02-13]
您正在将从内部content_tag调用返回的安全字符串连接到不安全的字符串"per" 。 这会导致不安全的字符串被转义。 确保所有字符串操作数都是html_safe, 或者生成的字符串是HTML安全的。 def plan_interval content_tag(:span) do "per".html_safe + content_tag(:span) do links = [] %w(week month year).each do |i| ...相关文章
更多- Why Ruby
- Rails 风格指导
- rails页面文件html.erb如何访问ActiveRecord的资源
- Rails常用插件
- Rails中的路由功能是如何对应的?
- 启动script/console时必须在前面加ruby,否则不能启动?
- 【学习笔记*原创】Ruby-China网站源代码包的本地安装
- Rails设置环境变量
- ruby中的字面常量$/是啥呢?
- ruby 全局变量
最新问答
更多- 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
- 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
- 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
- ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
- Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
- 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
- 功能包装避免重复(wrap of functions avoid duplicating)
- Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
- 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
- VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
- 大学电脑四级对初学者来说要多久能过
- 特殊字符删除?(Special characters remove?)
- Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
- 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
- Javaweb开发,技术路线是什么?该怎么写?
- JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
- 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
- Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
- 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
- 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
- “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
- maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
- 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
- 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
- java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
- 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
- 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
- 嵌套的模板可能性(Nested template possibilities)
- 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
- 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)