Rails: How to render repetitive form block?

元气小坏坏 提交于 2019-12-24 21:14:52

问题


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'

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'

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?

Thanks in advance.


回答1:


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



来源:https://stackoverflow.com/questions/12011459/rails-how-to-render-repetitive-form-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!