I''m trying to add some executable code to my blog posts. How can I include this inside of a blog post and eval it? Charlie bowman http://www.recentrambles.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060523/a3536408/attachment.html
On 5/23/06, Charlie Bowman <charlie@castlebranch.com> wrote:> I''m trying to add some executable code to my blog posts. How can I include this inside > of a blog post and eval it?In my controller I have a method that does this: @content = Content.find( :first, :conditions => [ ''active = 1 AND action = ?'', params[:id] ] ) Then in my view I have this: <%= render_template ''rhtml'', @content.body %> -- Greg Donald http://destiney.com/
I''m not sure I understand what you''re getting at. Currently I pull the post from the database, pass it to a template and the text is displayed. How could I embed a small block of code in that text that instead of being displayed, it will be executed? Forgive me if that''s what your already showing me. On Tue, 2006-05-23 at 09:51 -0500, Greg Donald wrote:> On 5/23/06, Charlie Bowman <charlie@castlebranch.com> wrote: > > I''m trying to add some executable code to my blog posts. How can I include this inside > > of a blog post and eval it? > > In my controller I have a method that does this: > > @content = Content.find( :first, :conditions => [ ''active = 1 AND > action = ?'', params[:id] ] ) > > Then in my view I have this: > > <%= render_template ''rhtml'', @content.body %> >Charlie Bowman http://www.recentrambles.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060523/d503aa89/attachment.html
Charlie Bowman wrote:> I''m not sure I understand what you''re getting at. Currently I pull the > post from the database, pass it to a template and the text is displayed. > How could I embed a small block of code in that text that instead of > being displayed, it will be executed? Forgive me if that''s what your > already showing me. > > On Tue, 2006-05-23 at 09:51 -0500, Greg Donald wrote: > >> >> <%= render_template ''rhtml'', @content.body %> >> > > > Charlie Bowman > http://www.recentrambles.comUse eval: <%= eval(@your_code_block) %> It''s a Ruby method that evaluates a string. -- Posted via http://www.ruby-forum.com/.
Charlie Bowman
2006-May-23 15:57 UTC
[Rails] Re: how to pull text from database and eval it?
what if I"m only evaling part of the blog post. Would I need to store the executable code in a seperate field? I wouldn''t want to eval the entire post. On Tue, 2006-05-23 at 17:37 +0200, Simen wrote:> Charlie Bowman wrote: > > I''m not sure I understand what you''re getting at. Currently I pull the > > post from the database, pass it to a template and the text is displayed. > > How could I embed a small block of code in that text that instead of > > being displayed, it will be executed? Forgive me if that''s what your > > already showing me. > > > > On Tue, 2006-05-23 at 09:51 -0500, Greg Donald wrote: > > > >> > >> <%= render_template ''rhtml'', @content.body %> > >> > > > > > > Charlie Bowman > > http://www.recentrambles.com > > Use eval: > > <%= eval(@your_code_block) %> > > It''s a Ruby method that evaluates a string.Charlie Bowman http://www.recentrambles.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060523/14196fba/attachment.html
On 5/23/06, Charlie Bowman <charlie@castlebranch.com> wrote:> I''m not sure I understand what you''re getting at. Currently I pull the post from > the database, pass it to a template and the text is displayed. How could I embed a > small block of code in that text that instead of being displayed, it will be executed? > Forgive me if that''s what your already showing me.render_template will evaluate any embedded code you include. -- Greg Donald http://destiney.com/
First put all the Ruby code that shall be executable into a tag: <ruby> list = [1, 2, 3] list.each do |one| print one end </ruby> So use a simple regex: @blog.content = @blog.content.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match| match = eval($1) end -- Judofyr -- Posted via http://www.ruby-forum.com/.
Charlie Bowman
2006-May-23 16:37 UTC
[Rails] Re: how to pull text from database and eval it?
Thanks, that will work perfectly! So simple, it went right over my head! On Tue, 2006-05-23 at 18:22 +0200, Magnus Holm wrote:> First put all the Ruby code that shall be executable into a tag: > <ruby> > list = [1, 2, 3] > list.each do |one| > print one > end > </ruby> > > So use a simple regex: > @blog.content = @blog.content.gsub(/\<ruby\>(.*?)\<\/ruby\>/) do |match| > match = eval($1) > end > > -- > > JudofyrCharlie Bowman http://www.recentrambles.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060523/75e48946/attachment.html