你可以嘗試以下
<div>
<% if @major.glance.length > 250 %>
<%= link_to_function truncate(@major.glance, length: 250), "$(this).parent().html('#{escape_javascript @major.glance}')" %>
<% else %>
<%= @major.glance %>
<% end %>
</div>
,或者如果你喜歡使用Read more
鏈接
<div>
<% if @major.glance.length > 250 %>
<%= truncate(@major.glance, length: 250) %>
<%= link_to_function '...Read more', "$(this).parent().html('#{escape_javascript @major.glance}')" %>
<% else %>
<%= @major.glance %>
<% end %>
<div>
UPDATE
由於在軌道4,5,link_to_function
已被廢棄,這是建議不要使用不顯眼的js,請使用以下內容
<div>
<% if @major.glance.length > 250 %>
<%= truncate(@major.glance, length: 250) %>
<%= link_to '...Read more', '', class: "read-more-#{@major.id}" %>
<script>
$('.read-more-<%= @major.id %>').on('click', function(e) {
e.preventDefault()
$(this).parent().html('<%= escape_javascript @major.glance %>')
})
</script>
<% else %>
<%= @major.glance %>
<% end %>
<div>
這是一個簡單的答案,爲我工作。 http://stackoverflow.com/a/26854494/5551783 – dgreen22 2016-06-08 15:36:39