I want to display a form partial within a shared partial layout form_layout
# views/layouts/shared/_form_layout.html.haml
= form_with(model: record, remote: true) do |form|
.fields
= yield
/// other stuff
# views/articles/_form.html.haml
# ?? want to access the form builder here
# views/articles/new.html.haml
= render partial: 'form', layout: 'layouts/shared/form_layout', locals: { record: Article.new }
How can I access the FormBuilder object inside the partial template?
You should try this-
= render layout: "layouts/shared/form_layout", locals: { record: Article.new} do
# block contents..
Related
I have an object that prebuilds a cached page. I call it like this:
str = ActionView::Base.new(Rails.configuration.paths['app/views'])
.render(partial: 'item_generator/item_with_modal',
locals: { item: #item, format: 'div',
create_external: #create_external,
view_file: #view_file })
How would I make it so that ApplicationHelper would be included so that helpers could be called?
I currently have something like:
<%=raw ApplicationController.helpers.js_clean(item.detail) %>
but would rather just have:
<%=raw js_clean(item.detail) %>
Use extend to include the helpers in the ActionView::Base instance:
str = ActionView::Base.new(Rails.configuration.paths['app/views']).tap do |v|
v.extend(ApplicationController.helpers)
end.render(
partial: 'item_generator/item_with_modal',
locals: {
item: #item,
format: 'div',
create_external: #create_external,
view_file: #view_file
}
)
I have a form which call a partial:
# form.html.haml
= simple_form_for(resource) do |form|
= form.input :foo
= render partial :some_fields, locals: { form: form }
# _some_fields.html.haml
= form.input :bar
My problem is I have to update the partial with ajax and I don't know how to create the form var from my controller:
def ajax_form_fields
render partial :some_fields, locals: { form: ? }
end
Any idea?
The only way I found to achieve this is to add this line at the beginning of _some_fields.html.haml:
- form ||= nil; simple_form_for(resource) { |f| form ||= f }
So, I don't need to pass the form var to the partial:
def ajax_form_fields
render partial :some_fields
end
How can I write this code in haml without repeating the render line?
- if i % 2 == 0
%section.wrapper-md.list
= render partial: 'property'
- else
%section.wrapper-md.list.background-gray
= render partial: 'property'
Thanks!!
Try something like this
%section.wrapper-md.list{class: ('background-gray' if i.even?)}
= render partial: 'property'
Also you could try using the cycle helper and no need for counters
%section.wrapper-md.list{class: cycle('', 'background-gray')}
= render partial: 'property'
I want to dynamically add classes to a container based on the name of the class that called my layout using the render method. Is it possible or do I need to pass it manually from each view?
File: emails/inbox.html.haml
render layout: 'shared/v3/panel' do
// whatever
Rendered HTML:
<div id="inbox" class="panel email_container"></div>
My temp solution is to do:
render layout: 'shared/v3/panel', locals: {class_panel: 'email_container', id_panel: 'inbox'} do
// whatever
But I want to do that dynamically.
Final solution: views/shared/_panel.html.haml
- # You need to use: "render layout", not "render partial" to pass the "do" block.
.container_content{class: "#{controller_name}_container #{local_assigns[:panel_class] ? panel_class : ''}", id: action_name}
- if local_assigns[:title]
.headline= local_assigns[:title]
= yield
- if local_assigns[:footer]
.footer= local_assigns[:footer]
- # Example of use:
-#= render layout: 'shared/v3/panel', locals: {title: 'Place', panel_class: 'my_custom_class'} do
-# - if #activity.place.present?
-# %p
-# = t('place')
-# = #activity.place
So I come across with a problem where I would like to save a partial as an html string and render another partial with that string but I keep getting a double render error
def fruits
fruits = Fruits.all
market_html = "#{render partial: 'market', locals: {fruits: fruits}}"
render partial: 'super_market', locals {market_html: market_html}
end
Do you want render_to_string?
market_html = render_to_string partial: 'market', locals: {fruits: fruits}