将参数传递到erb视图 [英] Passing parameters to erb view

查看:46
本文介绍了将参数传递到erb视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ruby和Sinatra将参数传递到erb视图。

I'm trying to pass parameters to an erb view using Ruby and Sinatra.

例如,我可以这样做:

get '/hello/:name' do
  "Hello #{params[:name]}!"
end

如何传递:name

How do I pass :name to the view?

get '/hello/:name' do
  erb :hello
end

我该如何读取view / hello.erb内部的参数?

And how do I read the parameters inside view/hello.erb?

谢谢!

推荐答案

只需在路由中将:locals传递给erb() :

just pass the :locals to the erb() in your routes:

get '/hello/:name' do
    erb :hello, :locals => {:name => params[:name]}
end

然后在视图/ hello中使用它.erb:

and then just use it in the views/hello.erb:

Hello <%= name %>

(在sinatra 1.2.6上测试)

(tested on sinatra 1.2.6)

这篇关于将参数传递到erb视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆