首页 \ 问答 \ 使用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中的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?

更新时间:2022-10-12 10:10

最满意答案

如果没有显式调用返回,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 %>

相关问答

更多

相关文章

更多

最新问答

更多
  • 在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)