【问题标题】:Insert code inside string [duplicate]在字符串中插入代码[重复]
【发布时间】:2016-09-15 19:57:24
【问题描述】:

我是 RoR 的新手。我正在尝试打印字符串'Logout'中登录的当前用户名的字符串以个性化消息,但我只能打印代码...

<% if user_signed_in? %>
      <%= link_to('Log out #{current_user.first_name}', destroy_user_session_path, :method => :delete) %>

任何帮助将不胜感激!

谢谢。

【问题讨论】:

  • 对字符串使用双引号

标签: ruby-on-rails ruby string ruby-on-rails-5 string-interpolation


【解决方案1】:

当你必须插入时使用双引号

<%= link_to("Log out #{current_user.first_name}", destroy_user_session_path, :method => :delete) %>

【讨论】:

  • (当然,这是一个骗局,但这不是对正确答案投反对票的正当理由。)
  • 谢谢!这行得通。我还不太熟悉 ROR 术语(字符串插值)来制定确切的问题。 +++
  • @DaveNewton 评论是关于什么的。我没听懂
  • 有人否决了您的回答,这是不恰当的行为。
【解决方案2】:

试试这个,

<%= link_to("Log out #{current_user.first_name}", destroy_user_session_path, :method => :delete) %>

您不能对单引号字符串使用插值。相反,您可以使用双引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多