HTML title attribute in Rails displaying only firs

2019-01-29 00:42发布

问题:

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>

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".

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?

回答1:

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! >

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!.

If you change your view to this:

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

Then your generated HTML should look like this:

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


回答2:

Insert double qutoes for title in the div tag

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


回答3:

Change this line:

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

to this:

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