Undefined Method in ruby on rails

I am new to Ruby on Rails and im stuck on something so simple but I just cant figure it out.

I scaffolded a Video model, controller and view and then created a Welcome controller for the home page.

I manually created an index.html.erb file in the welcome view folder and proceeded to route the home page to Welcome#index. All working fine for now.

Thing is when I define a method in the welcome_controller like this

def foo
   puts 'Hello'

and I call it in the welcome/index.html file like this

<%= foo %>

I get the following error: undefined local variable or method `foo' for #<#:0x39675d8>


Controller actions aren't view helpers, you'll want to put foo in either application_helper.rb or welcome_helper.rb to use it like you are and change it to remove the puts, like this:

def foo

This will insert Hello into your view (which is what I think you were expecting)

Based on your comments below, you should probably be using a scope in your model...

scope :highlighted -> { (where(highlight: true) }

... to return highlighted records from your controller...

@highlighted = Videos.highlighted

... and then iterate over @highlighted in your view...

<% @highlighted.each do |video| %>
<%= ...do something with video here... %>
<% end %>

please read more about all this here:

and specificially for scopes here...

You can't call arbitrary "things" in the controller.

What's exposed in the controller are its instance variables, e.g., @foo.

If you really want to call a method that uses puts, put it in a helper, e.g., welcome_helper.rb (or whatever the convention would be).

Note, however, that this won't do what you appear to believe it will, namely, put a "Hello" on the web page. You're basically writing directly to the console, not even the log file.

What specifically are you trying to accomplish?


 ? Undefined method, avatar and name for nil:NilClass
 ? undefined method `premium?' for nil:NilClass
 ? undefined method `default_image' for nil:NilClass
 ? C# WinForms - Determine when void method returns
 ? C# - Unable to add item to listview from method invoked from class
 ? GCC : Return statement from a void function in C
 ? How to call objective c method from c method iOS
 ? Creating multiple choice for user
 ? Does "return;" at the end of a void method in Java have ANY impact on performance?
 ? Java If else nested. public static void ()