Dividing text article to smaller parts with paging in Ruby on Rails

試著忘記壹切 提交于 2020-01-17 02:42:10

问题


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

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.

Sample code:

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/6648551/dividing-text-article-to-smaller-parts-with-paging-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!