问题
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