Rails same render partial as html string ? - ruby-on-rails

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}

Related

Is there a way to pass a local that is item dependent when rendering a collection rails 6

I have below line as the last statement in my controller action:
render json: {
html: render_to_string(
partial: "_item_content.html.haml",
collection: items, as: :item,
locals: {
seller: #seller
}, layout: false),
is_last_page: items.last_page?
}
In my partial I am doing item_discount = item.eligible_discount_for(current_user) which is what I want to avoid.
Is there a way to compute and pass eligible_discount as a local for each item while rendering the partial in the controller action? If not, how can I refactor this so I don't have to do the item_discount = item.eligible_discount(current_user) computation in the partial.
As I had to finish this quickly, below is what I did to keep the partial a bit cleaner and do the computation in the action, though I am not really sure it's the best way. Basically, instead of passing the collection to render, changed it to loop over the collection and aggregate the individual strings. Posting in case anyone else finds this useful, or can provide feedback to improve.
items_html = items.map do |item|
eligible_discount = item.eligible_discount_for(current_user)
render_to_string(partial: "item_content.html.haml",
locals: {
item: item,
seller: #seller,
eligible_discount: eligible_discount
},
layout: false)
end.reduce("", :+)
render json: {
html: items_html,
is_last_page: items.last_page?
}

How to pass a parameter from a partial layout to a partial?

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..

Rails Locals - Passing Connected Model Data

My model structure is pretty solid. I have MarketingDeliverySystem has_many MarketingSections. MarketingSections has_many MarketingVideos.
I have another segment: GroupDevelopment has_many GroupSections. GroupSections has_many GroupVideos.
I'm trying to use a partial to pass the variables, thus DRYing it all up.
I have the following that I'm trying to pass to the partial:
= render partial: '/sales_presentations/sales_presentation',
locals: { marketing_delivery_system: #marketing_delivery_system,
first_video: first_marketing_video(#marketing_delivery_system),
sales_presentation: #marketing_delivery_system}
Then in the partial I have the following:
.rounded-box-header.blue-bg #{sales_presentation.title}
ul
- sales_presentation.sections.ordered.each_with_index do |section, index|
- list_class = 'section show'
- list_class = 'section hide' if index != 0
li
= link_to section.title, '#', class: 'section', data: { id: section.id }
ul class="#{list_class}" data-section-id="#{section.id}"
- section.videos.ordered.each do |video|
li.video
= link_to video.title, '#',
class: 'video video-link',
data: { video: video.youtube_link,
sales_presentation: sales_presentation.title.parameterize }
.seven.columns
.row
div id="#{sales_presentation.title.parameterize}-container"
video {
id="#{sales_presentation.title.parameterize}-video-player"
class="video-js vjs-default-skin videos"
height=400
poster=""
controls preload='none'
data-default-url="#{first_video(sales_presentation)&.youtube_link}"
I previously had issues with sales_presentation.title at the top until I updated the locals.
My question/issue is how do I pass in through the locals to use for sales_presentation.sections instead to use #marketing_delivery_system.marketing.sections?
I thought I could just put that in through locals:
sales_presentation.sections: #marketing_delivery_system.marketing_sections but I end up with a massive syntax error.
I've also tried creating a partial view for these two and then changed sales_presentation throughout the view to mod. Then changed mod.sections to mod_section and setting that in the locals to mod_section: #marketing_delivery_system.marketing_section. The problem then gets into that I end up needing to hit video later in the iteration. So then that has the same issue.
You misunderstand the meaning of locals in partials.
Says we have
<%= render partial: 'image', locals: {size: #image.size, extension: #image.extension} %>
It means that in image partial now we can use local variable size and extension (keys) as #image.size and #image.extension (values).
Put in locals: {} all local variables you want.
So you can't write in locals sales_presentation.sections: #marketing_delivery_system.marketing.sections
But you can sales_presentation_sections: #marketing_delivery_system.marketing.section
Also you have problem with this code:
locals: { marketing_delivery_system: #marketing_delivery_system,
first_video: first_marketing_video(#marketing_delivery_system),
sales_presentation: #marketing_delivery_system }
marketing_delivery_system and sales_presentation will be with the same value.

simple_form_for: update a part of the content with ajax

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

rails haml if add class

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'

Resources