Rails 中的 HTML 标题属性仅显示第一个单词 [英] HTML title attribute in Rails displaying only first word

查看:12
本文介绍了Rails 中的 HTML 标题属性仅显示第一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到了一个奇怪的小问题.

Got a weird little problem.

我想在一个项目上创建文本悬停以显示该项目的描述",这是该项目模型的一个属性.所以我认为我基本上是这样做的:

I want to create the text hover on an item to display that item's "description," which is an attribute of that item's model. So I basically did this in my view:

<div title=<%= item.description %> ><%= item.name %></div>

奇怪的是,虽然所有模型调用都正常工作(item.name 和 item.description 调用了正确的东西),但当我悬停时,只显示 item.description 的第一个单词.换句话说,如果 item.description 是这是一个超级酷的项目!",当我用 item.name 将鼠标悬停在该 div 上时,悬停只会显示This".

The weird thing is, while all the model calls are working correctly (item.name and item.description are calling up the right things), only the first word of item.description is showing up when I hover. In other words, if item.description is "This is a super cool item!", when I hover over that div with item.name, the hover just says "This".

这可能与 :description 属性有关,该属性目前是文本类型(我认为这是用于长字符串,就像我的描述一样).但也许不是.

This may have something to do with the :description attribute, which is currently of type text, (which I thought was for long strings, like my descriptions). But maybe not.

知道为什么会发生这种情况,以及如何解决吗?

Any idea why this might be happening, and how to fix it?

推荐答案

我认为您需要在视图中的 HTML 中添加引用.如果您在生成的 HTML 上查看源代码,我敢打赌它看起来像这样:

I think you need to add quoting to the HTML in the view. If you view source on the generated HTML, I'm willing to bet it looks like this:

<div title=This is a super cool item! >

浏览器会将其解释为具有 title 且值为 Thisdiv,然后是名为 is 的属性code>、asupercoolitem!.

The browser will interpret that as the div having a title with the value This, and then attributes named is, a, super, cool, and item!.

如果您将视图更改为:

<div title="<%= item.description %>" >

那么你生成的 HTML 应该是这样的:

Then your generated HTML should look like this:

<div title="This is a super cool item!" >

这篇关于Rails 中的 HTML 标题属性仅显示第一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆