在帮助程序中将块传递给部分

时间:2015-04-06 00:47:14

标签: ruby-on-rails haml

我有这个代码,我试图从我的观点中调用,但没有成功。

# app/viwes/admin/index.html.haml
= panel title: "My title" do
  %h2 Hello!

# app/helpers/admin/suggestion_helper.rb
module Admin::SuggestionHelper
  def panel(locals, &block)
    render({partial: "admin/shared/panel"}, locals, &block)
  end
end

# app/views/admin/shared/_panel.html.haml
%h1= title
%div= yield

导致此错误'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.,为什么会这样?

我正在使用Rails 4.0.5

1 个答案:

答案 0 :(得分:3)

使用此代码解决了它

def panel(locals, &block)
  render(layout: "admin/shared/panel", locals: locals, &block)
end

我将partial键替换为layout,解决了问题。