1

I am new to Rails and want to achieve the following.

In my view, I have a very verbose set of expressions

<% @courses.each do |course| %>
  <%= course.name.squish.downcase.tr(" ","_").gsub(/[^0-9a-z_]/i, '')` %>
<% end %>

I want to define a new method called get_file_name and replace the second line of the original code with:

<%= course.get_file_name %>

Now, I have tried to define the new method in app/helpers/application_helper.rb, but it did not work.

Also, putting a scope in the app/models/course.rb was not successful. I tried to write scope :course_image, -> { name.squish.downcase.tr(" ","_").gsub(/[^0-9a-z_]/i, '') }

In php, I would simply define a new function. How can I do this in Rails 4?

mc9
  • 6,121
  • 13
  • 49
  • 87

3 Answers3

2

Since you want to call the method on an instance of a course, you should define an instance method on that class.

class Course
  def get_file_name
    name.squish.downcase.tr(" ","_").gsub(/[^0-9a-z_]/i, '')
  end
end

This should allow you to do what you want.

ptd
  • 3,024
  • 16
  • 29
2

Take a look at ptd's's answer if you want to call the method on the course instance. Just in case you were really excited to use a helper method you would need to pass the object as a parameter to the helper method:

module ApplicationHelper
  def get_file_name(course)
    course.name.squish.downcase.tr(" ","_").gsub(/[^0-9a-z_]/i, '')
  end
end

And then in your view you would write:

<%= get_file_name(course) %>
Community
  • 1
  • 1
Chris Kolodin
  • 1,685
  • 1
  • 11
  • 3
1

Objects

You have to remember that Rails is object orientated

This means that if you're looking to work with methods, you have to appreciate their relation to the objects you're hoping them to work on. The important thing to note here is that there are therefore two ways you could create the functionality you desire:

  1. Create a "helper" method
  2. Create an "instance" method

I'll explain both for you here:


Helper

"Helper" methods are pretty much the same as PHP functions, in that they are ambiguous.

They live in #app/helpers/[x]_helper.rb, and look like this:

#app/helpers/application_helper.rb
class ApplicationHelper
   def my_method
      ...
   end
end

The vital thing to note here is that as helpers are detached from your objects, you have to send through the data you wish to manipulate. This is very important because, as you'll see in a minute, it means you can use them to process a large variety of different data-sets

Here's how you'd use a helper method in your case:

#View
<%= get_file_name course.name %>

#app/helpers/application_helper.rb
class ApplicationHelper
  def get_file_name name
      name.squish.downcase.tr(" ","_").gsub(/[^0-9a-z_]/i, '')
  end
end

Instance Method

The next step is to look at creating an instance method for your Course model itself.

Because Rails (by virtue of being built on Ruby) is object orientated, every Model you create is basically an "object". This means that if you wanted to, you could introduce an instance method for each instance of that object:

enter image description here

#app/models/course.rb
class Course < ActiveRecord::Base
   def get_file_name
      name.squish.downcase.tr(" ","_").gsub(/[^0-9a-z_]/i, '')
   end
end

This will allow you to call: @course.get_file_name

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147