开发者

Cannot display variable from controller in erb file

I begin learning ruby and rails this week. So I follow some tutorial in the book I found in my school library.

I get to the point where I have main_controller.rb:

开发者_开发百科
class MainController < ApplicationController
  def welcome
    @my_var = 5
  end
end

and for my welcome.html.erb:

<h1>Welcome to the My Application</h1>
We currently have <%= @my_var %> visitors.

but when i run my server, all i got from localhost:3000/main/welcome is:

Welcome to the My Application We currently have visitors.

as if the everything in <%= %> was not executed. and I also try to insert puts 'welcome method is executed' to the main_controller.rb and it does print the line.

So any help? I thank you in advance, Fajarmf


Try <%=h @my_var %>


maybe you are rendering the page before you set the instance variables


There has no problem on your code, but when you change the code you need to refresh you browsers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜