【问题标题】:Rendering Partial from outside main/index.rhtml...Ruby on Rails从外部渲染部分 main/index.rhtml...Ruby on Rails
【发布时间】:2009-12-18 20:52:50
【问题描述】:

好的,我已经发布了一些与我的问题有关的内容,但我认为我已经缩小了范围。这是我所在的位置:

我的 index.rhtml 页面位于 /views/main 中,并且 main_controller 设置正确。我试图使这个页面成为各种仪表板,因此它需要引用多个其他视图来显示它们的 index.html.erb 页面。我将使用“提案”作为我们的示例。我想在views/main/index.rhtml 侧栏中显示提案views/proposals/index.html.erb 页面。我已经收集到你通过部分来做到这一点。

所以...我创建了一个文件 /views/proposals/_index.html.erb,它的代码与 views/proposals/index.html.erb 相同。

然后在我的views/main/index.rhtml 文件中我有以下代码:

<%= render :partial => @proposal %>

现在,我没有收到错误消息,只是没有显示任何内容。我的 routes.rb 文件中没有任何引用这个(我不认为)的东西,我怀疑这是问题所在。

很抱歉这个问题的冗余,但我什至不知道我在问什么。希望这可以帮助。

更新:

当我输入下面提到的&lt;%= render :partial =&gt; "proposals/index" %&gt; 时,我现在收到此错误:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #1):

1: <% @proposals.each do |proposal| %>
2: <div id="proposalindex">
3: <%= link_to_unless_current h(proposal.name), proposal %><br/>
4: <p5>Added <%= time_ago_in_words(proposal.created_at) %> ago | </p5

这部分在提案控制器中起作用,不确定这意味着什么。

【问题讨论】:

    标签: ruby-on-rails ruby controller routes


    【解决方案1】:
    <%= render :partial => "proposals/index" %>
    

    【讨论】:

    • 我刚做了,还是没去。走得更远了,但现在又出现了另一个错误。我将更新问题,以便您查看错误。
    • 您的MainController#index 中有@proposals 对象吗?如果你想列出所有的提案,你仍然需要在某个时候做@proposals = Proposal.all
    • 我没有。在那里展示它的最佳方式是什么?
    • 刚刚修好了。非常感谢大家。我很兴奋。
    猜你喜欢
    • 2013-06-24
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多