变量在 RoR 中未正确回显

发布于 2024-11-14 17:14:53 字数 917 浏览 3 评论 0原文

我正在按照 Rails 教程 开始(第 4000 次)使用 Ruby on Rails。我几乎是在快速阅读前几章,因为我已经阅读并输入了很多次这些内容,但我刚刚遇到了一个我似乎无法解决的小问题。

在其中一章中,建议我们创建一个辅助函数以使页面标题的显示更加动态。

助手看起来像这样:

module ApplicationHelper
  
  #return title on per page basis
  def title
    base_title = "Ruby on Rails Tutorial Sample App"
    if @title.nil?
      base_title
    else
      "#{base_title} | #{@title}"
    end
  end
end

我的控制器看起来像这样:

class PagesController < ApplicationController
  def home
    @title = "Home"
  end
  # more pages
end

最后,我的应用程序布局文件包含以下行:

  <title><%= @title %></title>

书籍说它现在应该“回显”(这是 PHP 行话吗? Tee-hee)以下标题:

Ruby on Rails 教程示例应用程序 | Home 为主页。但是,它仅呼应“主页”作为页面标题。

我在这里忽略了什么吗?我不认为我输入了任何错误或任何内容;对我来说一切看起来都很合乎逻辑,但它行不通。

多谢!

I'm following the Rails Tutorial to get started (for the 4000th time) with Ruby on Rails. I'm pretty much racing through the first couple of chapters, as I have already read and typed these quite a lot of times but I just ran into a minor problem which I cannot seem to solve.

In one of the chapters, it is suggested that we create a helper function to make the displaying of page titles more dynamic.

The helper looks like this:

module ApplicationHelper
  
  #return title on per page basis
  def title
    base_title = "Ruby on Rails Tutorial Sample App"
    if @title.nil?
      base_title
    else
      "#{base_title} | #{@title}"
    end
  end
end

My controller looks like this:

class PagesController < ApplicationController
  def home
    @title = "Home"
  end
  # more pages
end

And finally, my application layout file contains the following line:

  <title><%= @title %></title>

The books says that it should now "echo" (is this PHP lingo? Tee-hee) the following title:

Ruby on Rails Tutorial Sample App | Home for the homepage. However, it only echoes 'Home' as the page title.

Am I overlooking something here? I don't think I typed any errors or anything; everything looks fairly logical to me, however it won't work.

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2024-11-21 17:14:53

在您的视图中调用@title 指的是变量,而不是方法。由于变量@title 等于“Home”,这就是您得到的结果。要调用该方法,您应该尝试以下操作:

<title><%= title %></title>

Calling @title in your view refers to the variable, not the method. As the variable @title is equal to "Home", this is what you get. To call the method, you should try something like :

<title><%= title %></title>
我不会写诗 2024-11-21 17:14:53

我认为你从书中抄错了。 (好消息是,您遇到的问题在免费示例章节中。:)

这本书实际上的内容是:

<标题><%=标题%>

你拥有的是:

<标题><%= @title %>

使用@,可以直接查找变量。如果没有@,它将调用方法title()。 (我希望他们在函数调用后面加上 (),但那是 Ruby 惯用语。我只是不喜欢 Ruby 习惯用法。)

I think you've mis-copied from the book. (Good thing the problem you're having is in the free sample chapter. :)

What the book actually has is:

<title><%= title %></title>

What you have is:

<title><%= @title %></title>

With the @, that directly looks up the variable. Without the @, it calls the method title(). (I wish they had put () after the function call, but that is idiomatic Ruby. I just dislike the Ruby idiom.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文