【问题标题】:Ruby: How to pass a variable from server to index?Ruby:如何将变量从服务器传递到索引?
【发布时间】:2015-10-18 01:39:11
【问题描述】:
something = "0"

get "/" do
   erb :index, :locals => something
end

当我这样做并转到 localhost 时,它显示未定义的方法 `keys' for "0":String。我正在使用辛纳屈。如何将变量从服务器传递到索引?

【问题讨论】:

  • params 是一个哈希,所以它需要是something = {some_param: "0"},然后您可以通过locals 在视图中以some_param 的身份访问它。

标签: ruby post get sinatra erb


【解决方案1】:

您已经完成了一半,您只需将something 设为对象即可。

get "/" do
   erb :index, :locals => {:something => 0}
end

然后在你的索引中使用它:

Something: <%= something %>

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多