渲染 erb 模板时出现问题 [ruby]

发布于 2024-11-26 12:23:44 字数 553 浏览 0 评论 0原文

我正在阅读有关实例变量的旧教程。

但是我无法从控制器调用实例变量到视图。

代码是:

VIEW :

<html>
  <head>
    <title> title </title>
  </head>
  <body>
    <p>            line 1       </p>
    <p> <%=       @text   %>    </p>

  </body>  
</html>

CONTROLLER:

class MoviesController < ApplicationController
  def movies
    @text = "movietitle"
  end
end

当我加载页面时,它只显示第 1 行段落。

我如何调用实例变量@TEST?

Im reading an old tutorial about instance variables.

However i wasnt able to call de instance variable from the CONTROLLER to the VIEW.

The code is:

VIEW :

<html>
  <head>
    <title> title </title>
  </head>
  <body>
    <p>            line 1       </p>
    <p> <%=       @text   %>    </p>

  </body>  
</html>

CONTROLLER:

class MoviesController < ApplicationController
  def movies
    @text = "movietitle"
  end
end

When i load the page, it only shows the line 1 paragraph.

How can i call the instance variable @TEST?

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

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

发布评论

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

评论(2

叫思念不要吵 2024-12-03 12:23:44

@test 变量应包含 "movietitle",并且应正确打印这两个段落。

也许额外的空间(因为漂亮的印刷)把事情搞砸了?尝试:

<p>line 1</p>
<p><%=@text%></p>

编辑:

对于您所展示的内容,您的控制器方法必须命名为 index 而不是 movies

The @test variable should contain "movietitle", and it should properly print both paragraphs.

Maybe the extra space (because of pretty printing) is screwing things up? Try:

<p>line 1</p>
<p><%=@text%></p>

EDIT:

for what you showed, your controller method must be named index not movies

单身情人 2024-12-03 12:23:44

好的,根据您的视频,这是您的问题。
您向电影添加了一个名为 newaction 的新控制器方法。在浏览器中,您转到 /movies,它有效地调用电影的控制器方法索引并呈现电影的index.hmtl.erb。在index.html.erb中,您尝试获取在newaction中声明的变量,这是另一个完全不同的操作,当然newaction中的任何内容都无法从索引视图访问。
最大的问题是您似乎认为控制器方法就像从视图调用的函数,但事实并非如此。

  1. 了解 MVC 模型的工作原理:http://guides.rubyonrails.org/getting_started.html
  2. 必须在电影中创建 newaction.html.erb 视图
  3. 您需要将 newaction 操作添加到您的routes.rb 文件中

阅读以下内容: http://guides.rubyonrails.org/routing.html

paths.rb 中的代码如下所示。


resources :movies do
  get 'newaction'
end

如果你执行这些操作并转到 /movies/newaction 并在 newaction.html.erb 中显示标题,它将起作用。

Ok based on your video, here is your problem.
You add a new controller method to movies called newaction. In your browser you go to /movies which effectively, calls the controller method index for movie and renders the index.hmtl.erb for movies. Inside index.html.erb you try to get the variable you declared in newaction, this is another completely different action and of course anything in newaction is not accesible from the index view.
The biggest problem is you seem to think controller methods are like functions which are called from view, they are not.

  1. Read how the MVC model works: http://guides.rubyonrails.org/getting_started.html
  2. You have to create a newaction.html.erb view in movies
  3. You need to add the newaction action to your routes.rb file

Read this: http://guides.rubyonrails.org/routing.html

The code in routes.rb will look like.


resources :movies do
  get 'newaction'
end

if you do these and you go to /movies/newaction and inside newaction.html.erb display the title, it will work.

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