问题
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