Forum: Ruby on Rails .rhtml in the model rather than the controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael J. (Guest)
on 2005-12-15 20:49
Hi, I'm developing a mini-programming language for research surveys.

[code]
question S1 {
	label: The first few questions are just to help us categorize you.
	label: Do you or does any member in your household work for... (MARK
ONE ONLY FOR EACH.)

	grid {
		col {
			1 Yes
			2 No
		}

		row {
			1 An advertising agency
			2 A public relations company
			3 A marketing research firm or department
			4 An office products company
			5 A manufacturer or retailer of writing instruments
			6 A manufacturer of packaging or adhesives
		}
	}
}

invalid if in("THISQUESTION","1")
[/code]

I'd like to use .rhtml-style codes for pre-processing macros like this:

[code]
<% specialQuestions.each do |q| %>
question <%= q.name %> {
     	label: Where do you purchase writing instruments like <%= q.type
%> for use at home, school or your office?  (MARK ALL THAT APPLY.)

	multiple {
		1 Office products super stores such as Staples, Office Depot, or
Office Max.
		2 Office supply catalogs such as Viking, Corporate Express, Boise,
Staples, Office Depot, or Office Max catalogs
                3 Other
        }
}
<% end %>
[/code]

(You can see how useful this feature would be for a large survey.  One
could build hundreds of similar questions from small chunks of code.)

If the compile phase was in the controller, I could just do:

[code]
@script = render_to_string(:inline => @script)
[/code]

But that doesn't work if I'm in the model, right?  How can I render a
document to a string while in the model?
Jules (Guest)
on 2005-12-15 21:23
Michael J. (Guest)
on 2005-12-15 21:34
Jules wrote:
> .rhtml is parsed bu ERb:
> http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/index.html

Genius.  This is exactly what I needed.  Thank you, Jules!
This topic is locked and can not be replied to.