Welcome Guest | Login

executing ruby code stored in the database

Is there a way to make the view execute ruby code that is stored in the database when the field containing it is displayed?

For example, when I entered some content into the database I used <% link_to ...%> for the links but when the actual page is displayed it doesn't execute this code.  The view just prints it out literally.

2008-08-02 07:08 AM

Hi Anrake,

You can execute ruby codes given in database with the help of stored procedures.

Eg:-

CREATE PROCEDURE mydb.my_stored_procedure()
BEGIN
$$ Insert Ruby Code $$
END


The stored procedure can be called from the rails application by executing the Call function

eg:- CALL my_stored_procedure()


2008-08-02 09:04 AM

Anrake,

You might also try render :inline. I'm currently using this in a lot of places (probably more than I should be). For example, a page with some HTML/Erb content:

$ script/console
>> puts Page.find(1).content
<p>Check out my awesome <%= link_to 'blog', blog_path %>!</p>
and a view to render it:

# app/views/page/show.html.erb
<%= render :inline => @page.content %>

2008-08-04 09:27 PM

Haha, I just started doing this and it's great fun. You can use Ruby's 'eval' method to execute a string as Ruby code. The second argument, 'binding', determines what context the code will execute in. You can try it in the console:

$ script/console
Loading development environment (Rails 2.1.0)
>> s = 'foo'
=> "foo"
>> eval "s='bar'", binding
=> "bar"
>> puts s
bar
=> nil
>> exit
See http://www.ruby-doc.org/core/classes/Kernel.html#M005948.

2008-08-20 11:17 AM

Thanks guys!  render :inline seems to work best for me.

2008-08-24 06:59 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!