Reputation: 14902
I'm trying to create a helper function that strips all stopwords from the content field of data. I've used the basic scaffolding, like so:
rails generate scaffold MyData pageId:integer content:text
I've added a private method in the controller as such:
private
STOP_WORDS = %w{a am an as at be by do go he i if in is it me my no of on or so to un up us we}
def remove_stop_words(lowercase_string)
lowercase_string.gsub(/\b(#{STOP_WORDS.join('|')})\b/mi, '')
end
and now I'm wondering, since the controller is
def index
@tube_data = TubeDatum.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @tube_data }
end
end
and the corresponding view is
<% @tube_data.each do |tube_datum| %>
<tr>
<td><%= tube_datum.pageId %></td>
<td><%= tube_datum.content %></td>
....
how to go about making each tube_data.content stripped?
Thanks!
Upvotes: 0
Views: 672
Reputation: 4737
Move that code from your Controller into application_helper.rb in the app/helpers folder and wrap it in a method called stripstopwords.
Then in your view go stripstopwords tube_datum.content
Upvotes: 0
Reputation: 6857
Add the function in: app/helpers/application_helper.rb
STOP_WORDS = %w{a am an as at be by do go he i if in is it me my no of on or so to un up us we}
def remove_stop_words(lowercase_string)
lowercase_string.gsub(/\b(#{STOP_WORDS.join('|')})\b/mi, '')
end
In the view:
<%= remove_stop_words(tube_datum.content) %>
Upvotes: 4