0

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!

2 Answers 2

4

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) %>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.