【问题标题】:String interpolation not working in Sinatra redirect [duplicate]字符串插值在 Sinatra 重定向中不起作用 [重复]
【发布时间】:2018-05-16 23:38:18
【问题描述】:

当我使用这个方法时:

patch '/posts/:id' do
  post = Post.find(params[:id])
  post.update(name: params[:name], content: params[:content])
  redirect to '/posts/#{post.id}'
end

我收到错误ERROR URI::InvalidURIError: bad URI(is not URI?): http://localhost:9393/posts/#{post.id}

但是当我硬编码 id 或像这样连接字符串时: redirect to '/posts/' + post.id.to_s

它工作正常。我错过了什么明显的东西吗?

【问题讨论】:

    标签: ruby sinatra


    【解决方案1】:

    在 ruby​​ 中,用单引号声明的字符串字面量不会被插值,这与双引号字面量不同:

    "#{42}"
    #⇒ "42"
    '#{42}'
    #⇒ "\#{42}"
    

    所以,只需将redirect to 的参数中的单引号改为双引号即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多