Sinatra and computed view name
I'm completely new in Ruby and Sinatra so please forgive the trivial question:
I wanted to compute the view name instead of just passing in a symbol. I wanted the same action to return different views depending on the current state. There's like 20 different states so putting a good naming convention allows me to express the view name as a string very easily:
get "/page" do
erb "page-#{session[:page]}"
end
When I do that all I get is the string instead of the rendered view. Can anyone explain to me how I could do that in开发者_JAVA百科 Sinatra?
I'd say you're probably looking for String#to_sym
. I didn't test now, but all the examples say erb
receives a symbol argument, not a string - so try this:
erb "page-#{session[:page]}".to_sym
or equivalently
erb :"page-#{session[:page]}"
If you pass string to erb
it tries to render that string directly, not seeking for view with corresponding name. Converting string to symbol will help.
精彩评论