Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RubyonRails-截断为特定字符串_Ruby_Ruby On Rails 3 - Fatal编程技术网

RubyonRails-截断为特定字符串

RubyonRails-截断为特定字符串,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,澄清:帖子的创建者应该能够决定何时进行删节 我在我的博客中实现了类似Wordpress的[---更多---]功能,并具有以下帮助功能: # application_helper.rb def more_split(content) split = content.split("[---MORE---]") split.first end def remove_more_tag(content) content.sub(“[---MORE---]", '') end 在索引视图中,帖子正文将

澄清:帖子的创建者应该能够决定何时进行删节

我在我的博客中实现了类似Wordpress的[---更多---]功能,并具有以下帮助功能:

# application_helper.rb

def more_split(content)
split = content.split("[---MORE---]")
split.first
end

def remove_more_tag(content)
content.sub(“[---MORE---]", '')
end
在索引视图中,帖子正文将显示[---更多---]标记之前(但不包括)的所有内容

#index.html.erb
在show视图中,除了[---更多---]标记外,post正文中的所有内容都将显示

#show.html.erb
这个解决方案目前对我来说没有任何问题。 由于我还是一名编程初学者,我一直在想是否有一种更优雅的方法来完成这项工作

你会怎么做

谢谢你抽出时间


这是更新版本:

# index.html.erb
<%=raw truncate(post.rendered_body,  
                :length => 0, 
                :separator => '[---MORE---]', 
                :omission => link_to( "Continued...",post)) %>
#index.html.erb
0, 
:separator=>'[---更多---]',
:省略=>link_to(“Continued…”,post))%>
…并在“显示”视图中:

# show.html.erb
<%=raw (@post.rendered_body).gsub("[---MORE---]", '') %>
#show.html.erb

您可以在索引页上使用一个helper函数,该函数只获取字符串中的前X个字符。因此,它看起来更像:

<%= raw summarize(post.rendered_body, 250) %>

我会使用简单的截断,它有你需要的所有选项

truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
# => "And they f... (continued)"
更新

在查看了注释并挖掘了一些文档之后,似乎是
:separator
完成了这项工作

从文档:

Pass a :separator to truncate text at a natural break.
参考请参见


在“展示”页面上,您必须使用
gsub

我试过了,发现这是最好、最简单的

def summarize(body, length)
    return simple_format = body[0..length]+'...'
end

s = summarize("to get the first n characters in your post. So, then you don't have to deal w/ splitting on the [---MORE---]  post.body.",20)


ruby-1.9.2-p290 :017 > s
=> "to get the first n ..." 

我需要一个我可以在这个ERB呼叫模板上使用的。它只是一个简单的字符串:

<%= @page_info.user_name %>

我试过
-2)


但它实际上会重复长用户名两次,或者根据号码的不同而有所不同。我只想允许显示10个字符的用户名。

是的,我以前使用过这种方法。但是如果您想让用户决定何时进行截断,该怎么办呢?truncate(post.rendered_body,:separator=>'[--MORE--]')这似乎只在您定义长度时起作用:truncate(post.rendered_body,
:separator=>'[--MORE--]',:length=>0)
我用现在有效的代码更新了最初的问题。谢谢你的帮助!
Pass a :separator to truncate text at a natural break.
truncate(post.rendered_body, :separator => '[---MORE---]')
def summarize(body, length)
    return simple_format = body[0..length]+'...'
end

s = summarize("to get the first n characters in your post. So, then you don't have to deal w/ splitting on the [---MORE---]  post.body.",20)


ruby-1.9.2-p290 :017 > s
=> "to get the first n ..." 
<%= @page_info.user_name %>