Pasar parámetros a la vista erb


Estoy tratando de pasar parámetros a una vista erb usando Ruby y Sinatra.

Por ejemplo, puedo hacer:

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

¿Cómo paso :name a la vista?

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

Y cómo leo los parámetros dentro de view/hello.erb?

Gracias!

Author: Fábio Perez, 2011-07-18

3 answers

Simplemente pasa el: locals a erb () en tus rutas:

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

Y luego solo úsalo en las vistas / hola.erb:

Hello <%= name %>

(probado en sinatra 1.2.6)

 73
Author: Pavel Veller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-19 04:46:43

No estoy seguro de si esta es la mejor manera, pero funcionó:

get '/hello/:name' do
  @name = params[:name]
  erb :hello
end

Entonces, puedo acceder a :name en hola.erb usando la variable @name

 15
Author: Fábio Perez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-18 19:27:24
get '/hello/:name' do
  "Hello #{params[:name]}!"
end

No se puede hacer esto en rutas.

Desea establecer los parámetros en el controlador.

app/controllers/some_controller.rb

def index
    params[:name] = "Codeglot"
    params[:name] = "iPhone"    
    params[:name] = "Mac Book"      
end

app/views/index.html.erb

<%= params[:name] %>
<%= params[:phone] %>
<%= params[:computer] %>
 -7
Author: thenengah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-18 19:14:43