通过Ruby on Rails中的分页将文本文章划分为较小的部分 [英] Dividing text article to smaller parts with paging in Ruby on Rails

查看:43
本文介绍了通过Ruby on Rails中的分页将文本文章划分为较小的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这次,我遇到了将文本文章分成较小部分的问题.我不需要弄清基于单词计数等的自动"算法.我需要的是类似于内置Wordpress WYSIWYG编辑器(特殊的分页标签)的功能.

This time I've got problem with dividing text article into smaller parts. I don't need to figure out "automatic" algorithm based on words counting or something. All I need is something similar to function which is build-in Wordpress WYSIWYG editor (special breaking page tag).

到目前为止,我只想到了一种解决方案.我不想在数据库中划分特定的文章.我只想在文章中放置一些标签,然后在show方法中将其划分为数组.

I thought out only one solution so far. I don't want to divide specific article inside my database. I just want to place some tag inside article and divide it to array in show method.

示例代码:

#controller
@art = Article.find(:id)
if @art.value.contains?('<breaker>')
  @parts = art.value.split('<breaker'>)
end

session[:current_part] = params[:current_part] ? params[:current_part] : @parts.first
...
render 

#view
<%=h @parts[session[:current_part]] %>

对您来说听起来如何?有道理吗?无法等待一些建议.

How it sounds for you? It makes any sense? Cant wait for some advices.

推荐答案

使用HTML注释可能会更好,这样就不会影响页面的验证.

It may be better to use an HTML comment so it does not affect the validation of the page.

在Rails视图中,在显示断路器之前文本的模板中,您可以像在示例代码中那样拆分内容.我将在Rails助手模块中执行此操作,以便可以重复使用.

In your Rails views, in the templates that show text before the breaker, you can split your content like what you have in the example code. I would perform this in a Rails helper module so it can be reused.

要查看完整的文章,如果传递了参数更多",则您的助手方法将返回完整的内容.该代码可能看起来像这样:

To view the full article, your helper method will return the full content if the parameter "more" is passed. The code may look something like this:

# controller
def show
  @article = 'Before the break<!--more-->After the break'
end

# app/helpers/application_helper.rb
def show_more(article)
  params[:more] ? article : article.split('<!--more-->').first
end

# show.html.erb
<%= show_more(@article) %>

通常最好的做法是将应用程序逻辑保留在模型和帮助文件中,并使控制器尽可能简单.

It is generally good practice to keep the application logic in the model and helper files, and keep your controllers as simple as possible.

这篇关于通过Ruby on Rails中的分页将文本文章划分为较小的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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