render erb from database into view problem please

2020-04-21 11:37发布

问题:

i am saving some erb in my database and rendering it in the view like this:

erb = ERB.new(content)
render :text => erb.result

I am getting errors when trying render erb that has the image_tag in the erb saved in the database. The error is :

undefined method `image_tag' for main:Object

Anyone help on this ? i also get the error with the stylesheet_link_tag ?

Thank alot rick

回答1:

I think that you would need to pass the optional binding parameter to the ERB::render method. This effectively provides the local variables in the scope of the ERB template. In other words the binding needs to provide the image_tag variable to the template.

I don't know what 'content' is in your case but the following will pass the binding from the 'parent' view assuming that @obj.image_tag is visible from that view:

<%= ERB.new("image tag - \<\%= @obj.image_tag \%\>").result(binding) %>


回答2:

It's because you haven't helper in your controller. You need include all Helper do you use.