【问题标题】:Render from Helper yields "Missing Template" error从 Helper 渲染会产生“缺少模板”错误
【发布时间】:2011-06-27 01:14:05
【问题描述】:

在我的优惠券视图中,我调用了 helpers/coupons_helper.rb 中的一个方法,该方法又调用了一个partial,目的是返回它的输出。但我收到“缺少模板”错误。以下是相关代码:

views/coupons/show.html.erb:
  ...
  <%= form_for(@coupon) do |f| %>
    <% new_include(f) %>
  <% end %>

helpers/coupons_helper.rb:
  def new_include(f)
    new_object = Participation.new
    fields = f.fields_for(:participation, new_object, :child_index => :new_include) do |builder|
      render "_locations_include", :f => builder
    end
  end

views/coupons/_locations_include.html.erb:
  Include: <%= f.select :participant_id, @groups %><br>

错误信息是:

Missing partial coupons/_locations_include with {:handlers=>[:erb, :builder, :rjs, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/Users/jackrg/Documents/YummiApps/yummi1/app/views"

问题原来是“new_include”中“render”中的前导“_”。问题解决了。

【问题讨论】:

    标签: ruby-on-rails renderpartial view-helpers


    【解决方案1】:

    您不要在render 调用中为下划线添加前缀:

    render "locations_include", :f => builder
    

    附带说明一下,在文件名中添加_include 后缀可能有点多余,因为以下划线开头的 .erb 文件暗示它是部分文件。

    【讨论】:

    • 我同意你的观点,但我有两个部分:一个称为locations_include,另一个称为locations_exclude。我确实在发布后大约 15 分钟(在发布前看了一个小时之后)发现我有一个无关的“_”,但我没有足够的状态来发布我自己的问题的答案,直到 8 小时过去了,所以我不得不接受同龄人对我的蔑视;)无论如何,感谢您的观察。
    猜你喜欢
    • 2014-02-13
    • 1970-01-01
    • 2018-02-26
    • 2015-11-21
    • 2013-01-27
    • 2011-11-09
    • 2018-10-25
    • 2021-02-01
    • 2013-05-13
    相关资源
    最近更新 更多