Rails:如何呈现重复的表格块? [英] Rails: How to render repetitive form block?

查看:45
本文介绍了Rails:如何呈现重复的表格块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了不重复我自己,我想创建一个以特定方式呈现表单块(文本字段或文本区域)的函数.我想要这样的结果(使用Haml和twitter-bootstrap):

In order to not repeat myself, I would like to create a function that render a form block (text field or text area) in a specific way. I want a result like this one (using Haml, and twitter-bootstrap):

.form-block.input-prepend
    %span.add-on>
        %i.icon.icon-home
    = f.text_field :name, :value => @store.name, :placeholder => 'Company Name'

为此,我创建了一个文件views/layouts/_form-block.html.haml,在其中插入以下代码:

To do that, I created a file views/layouts/_form-block.html.haml where I inserted the following code:

.form-block.input-prepend
    %span.add-on>
        %i{ :class => "icon icon-#{icon}" }
    = yield

然后我在视图中使用以下代码调用该块:

And I call the block in my view using:

- render :template => 'layouts/_form-block', :locals => { :icon => 'home' } do
    = f.text_field :name, :value => @store.name, :placeholder => 'Company Name'

但是它不起作用.我出现以下错误'nil' is not an ActiveModel-compatible object that returns a valid partial path.

But It doesn't work. I have the following error 'nil' is not an ActiveModel-compatible object that returns a valid partial path.

你有什么主意吗?这是最好的方法吗?

Do you have any idea? Is this the best way to do?

谢谢.

推荐答案

如果要使用= yield传递块,则需要渲染为布局.使用render :layout =>代替render :template =>.

If you want to use = yield to pass a block, you need to render as a layout. Use render :layout => instead of render :template =>.

这篇关于Rails:如何呈现重复的表格块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆