I have a series of some strings, that i want to convert to render-partial.
For example. I have a @post.content, which contains
Lopem ipsum dolor sic amet *form_34e141443439d1000000* Lorem dolorem *form_f97391de6275724201000000*
Which i want to process.
I tried
content.gsub(/\*form_(.*?)\*/, render(partial: 'forms/render_form', object: CustomForm.where(_id: '\1').first, as: 'form_item'))
But that does not do anything correct, results only in "undefined local variable or method `form_item' for #<#:0xb53a2810>" error.
But if i try
content.gsub(/\*form_(.*?)\*/, render(partial: 'forms/render_form', object: '\1', as: 'form_item'))
and try <%= form_item %> in partial, it returns to me an correct value - f97391de6275724201000000
and if i try
content.gsub(/\*form_(.*?)\*/, render(partial: 'forms/render_form', object: CustomForm.where(_id: 'f97391de6275724201000000').first, as: 'form_item'))
it works just like i want it to - gets me a form model to process.
QUESTION:
How do i throw \\1 value from /*form_(.*?)*/ regexp to CustomForm.where(_id: '\\1').first ?
You can use the block form of the gsub
method where the pattern match is available as the $1
variable:
content.gsub(/\*form_(.*?)\*/) do |match|
render(partial: 'forms/render_form',
object: CustomForm.where(_id: $1).first, as: 'form_item'))
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.