开发者

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

开发者 https://www.devze.com 2023-03-19 03:24 出处:网络
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

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 ou开发者_运维技巧t 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消