简体   繁体   English

将块传递给助手中的部分

[英]Pass block to partial in helper

I've this code which I'm trying to call from my view, without success. 我已经尝试从我的角度调用此代码,但没有成功。

# 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

This results in this error 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. 这导致此错误'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path. , why is that? , 这是为什么?

I'm using Rails 4.0.5 . 我正在使用Rails 4.0.5

Solved it using this code 使用此代码解决了

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

I replaced the partial key with layout , which solved the problem. 我将partial关键字替换为layout ,从而解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM