5

Can anyone explain to me why I am getting this error? Rails is about conventions. Is there a more conventional way of doing what I'm trying to do below?

undefined local variable or method `hello_world' for #<#<Class:...>:...>

Here are my files:

welcome_controller.rb

class WelcomeController < ApplicationController
  def hello_world
    "Hello, World"
  end
end

welcome/index.html.erb

<%= hello_world %>

routes.rb

Rails.application.routes.draw do
  get 'welcome/index'
  root 'welcome#index'
end
4
  • What is your question? Commented May 1, 2014 at 20:17
  • 1
    Have you run through a Rails tutorial? Commented May 1, 2014 at 20:17
  • change subject - how to call controller methods inside a view?..suggestion.. Commented May 1, 2014 at 20:36
  • I followed along with the Beginning Guide to Rails and the Code School Rails for Zombies, but I'm still confused. I thought I was doing it right. Commented May 1, 2014 at 23:40

5 Answers 5

7

Or do as :

class WelcomeController < ApplicationController
 helper_method :hello_world
 def hello_world
    "Hello, World"
 end
end

Now call it inside the view :

<%= hello_world %>

Read helper_method

Declare a controller method as a helper, to make the hello_world controller method available to the view.

Sign up to request clarification or add additional context in comments.

1 Comment

Just so I'm clear, cause I don't really understand... if I wanted to do a basic hello world without the helper_method, SHOULD I have just created a class, and given hello_world inside the class? Reading up on helper_method now, but not sure why I need it.
4

You can use a helper to allow you to write that in your ERB:

module WelcomeHelper

  def hello_world
     "Hello, World"
  end

end

and now, your ERB should work:

<%= hello_world %>

2 Comments

@UriAgassi inclusion of WelcomeHelper in Controller would only be required if OP wants to access hello_world in Controller.
@UriAgassi You need to use helper WelcomeHelper inside the controller.. Do that to make the answer complete, so that future readers not need to go and search it inside the doc..
2

hello_world is action defined in controller and routes You need to define variable(s) in your action. This variable(s) will be accessible in the view

class WelcomeController < ApplicationController

 # this is action
 def index
    # this is variable
    @hello_world = "Hello, World"
 end

end

# view index.html.erb
# this calls variables defined in action
<%= @hello_world %>

Update1:

And if you define your route to welcome#index the action name in controller should be index!

4 Comments

This will not work unless you set the variable in WelcomeController #index action. As OP wants to access that particular value in index.html.erb. Change you action name to index instead of hello_world
@KirtiThorat instance variables can be used inside a view..why should it to be moved inside #index ?
Because OP is rendering index.html.erb and NOT hello_world.html.erb
@ArupRakshit Try it in any of your Rails application and let me know if it works for you.
0

This is more Rails-convention for you:

#config/routes.rb
root to: "welcome#index"
resources :welcome

#app/controllers/welcome_controller.rb
Class WelcomeController < ApplicationController
    def index
       #-> will render #app/views/welcome/index.html.erb
    end
end

#app/views/welcome/index.html.erb
Hello World
<%= hello_world %> <!-- Use `@Arup`'s answer to create a helper method -->

Comments

0

You can use print 'ff'. it simply work for me :)

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.