這不是你如何使用控制器方法見http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper_method。在Rails中,控制器上定義的方法用於「設置」特定視圖所需的數據,或處理給定的請求。他們不應該被視圖直接調用。
對於你想要做的事情,你需要添加一個輔助方法到WelcomeHelper
。所以,假設你想http://yourapp.dev/welcome/
輸出上面的消息,這是你所需要的:
# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def index
# Explicitly defining the `index` method is somewhat redundant, given
# that you appear to have no other logic for this view. However, I have
# included it for the sake of example.
end
end
# app/views/welcome/index.html.erb
<%= greeting %>
# app/helpers/welcome_helper.rb
class WelcomeHelper
# All methods in WelcomeHelper will be made available to any views
# that are part of WelcomeController.
def welcome
if (@current_user)
# You may need to change this to something like `@current_user.name`,
# depending on what @current_user actually is.
'¡Bienvenido' + @current_user + ' a nuestra web!'
else
"¡Bienvenido invitado, no dude en registrarse!"
end
end
end
謝謝,這是問題所在。 – Ivanhercaz 2011-06-09 16:02:41