Here is the code that works for me in views/posts/show.html.erb file:
<% require 'rmmseg' %>
<% RMMSeg::Dictionary.load_dictionaries %>
<% text = "你好" %>
<% algor = RMMSeg::Algorithm.new(@post.content) %>
<% loop do %>
<% tok = algor.next_token %>
<% break if tok.nil? %>
<% text2 = tok.text.force_encoding('UTF-8') %>
<%= "#{text2}" %>
<% end %>
I am new to rails so I need help knowing where to put this code, or similar, in the framework so that it saves the post with the spaces in the database. Should it be in the controller? If so, how would I do it?
I was trying and failing with this:
def create
@post = Post.new(params[:post])
require 'rmmseg'
RMMSeg::Dictionary.load_dictionaries
algor = RMMSeg::Algorithm.new(@post.content)
loop do
tok = algor.next_token
break if tok.nil?
text2 = tok.text.force_encoding('UTF-8')
@post.content = "#{text2}"
end
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
I obviously have know idea what I am doing yet.
I called the Rails Hotline and they gave me the hint to put it in the model using a callback (before_save, after_save, etc.)
That worked for me.