@@ coffee_hour = Plan.find_by_event(:coffee_hour)…是否可以使用这样的变量链接?

问题描述 投票:0回答:1

我的链接表有五个不同的事件,我使用了局部变量,因为它们分散在页面中。例如,“ coffee_hour”和“ wine_time”。每个事件都有一个URL和一个时间。在我的控制器中,我有:

 @coffee = Link.find_by (event: "coffee")
 @wine = Link.find_by (event: "wine")

而且我的零件看起来像这样:

  <p>Join us for a cup of coffee... <%= link_to "by clicking here.", coffee.url %> ...</p>

以及我的主页上的代码:

  <%= render partial: "links/coffee", locals: { coffee: @coffee } %>

所有五种事件类型都有单独的部分。但是上面的结果导致nil Nil:Class出现“身份不明的方法'url'。当我插入短语集合:@links时,在本地人面前我没有收到错误,但也没有返回任何内容。我的代码在控制台中有效,但我的Rails语法必须关闭。控制台中的代码:

  @coffee = Link.find_by(event: "coffee")
  @coffee.url # => correct url
ruby-on-rails partials class-instance-variables
1个回答
0
投票

您的控制器代码中有错误。更改为此:

@coffee = Link.find_by(event: ‘coffee’)
@wine = Link.find_by(event: ‘wine’)

[您现在可以看到,您仅在字符串周围有括号。您要么需要event: string周围的括号,要么将它们全部删除。

© www.soinside.com 2019 - 2024. All rights reserved.