从另一个控制器页面访问模型变量

时间:2021-08-03 15:59:59

Setting which functions as a post. It has the variables: Title:string Description:text, Photo:image and Paragraph:text.

设置哪个功能作为帖子。它有变量:标题:字符串描述:文本,照片:图像和段落:文本。

I have another controller which controls my Home, About, News and contact.
On my Home, I'm trying to access the title of the setting with the id 3. I can't figure it out. Do i need to define the controls for the settings in the page controller as well? How can I access it?.

我有另一个控制器来控制我的家,关于,新闻和联系。在我家,我正在尝试访问ID为3的设置标题。我无法弄明白。我是否还需要为页面控制器中的设置定义控件?我该如何访问它?

Here is my code at the Home,

这是我家的代码,

<% @setting.(id[3]) do |setting| %>
<h2><%= setting.title %></h2>
<% end %>

Error: Undified local varible or method 'id' for #<#

错误:#<#的Undified local varible或method'id'

Would appreciate some help, or a push in the right direction. Thanks :)

会欣赏一些帮助,或推动正确的方向。谢谢 :)

1 个解决方案

#1


3  

If you want to display single record then there is no need to have loop, so you can try this.

如果你想显示单个记录,那么就不需要循环,所以你可以试试这个。

<% setting = Setting.find(3) %>
<h2><%= setting.title %></h2>

#1


3  

If you want to display single record then there is no need to have loop, so you can try this.

如果你想显示单个记录,那么就不需要循环,所以你可以试试这个。

<% setting = Setting.find(3) %>
<h2><%= setting.title %></h2>