Rails Models - Can they have helpers? that allow for rendering a view?
In a rails 3 model.开发者_JAVA百科 can I use a helper? If so how. And in that helper can I render a view allowing me to store the view in the db?
Thanks
If storing your views in the DB is what you are after you shouldn't have to edit the model. You can use some code like:
render :inline => erb.view
erb is the view record and erb.view is the actual erb text.
Perhaps it would help if you told us why? Why do you want to store a view's raw text in the db?
"output of the view after it's been processed." I need to store the results of the render in the model. Is there a way to call the controller from the model to get the output?
Are you wanting to render custom or dynamic views based on a model? Why does this view need to be tied so closely to the model? Surely if you're wanting to store some kind of raw html/ruby in the db then you're going to try and get it back out again later down the road. Have you considered taking another route altogether?
"Is there a way to call the controller from the model to get the output?" Really this is the part that makes it seem like you aren't quite looking at it 'the rails way', but perhaps if we knew more about why and what you're trying to do, we can tell you if there even is a rails way. (I say this because with the MVC design convention used by rails, models don't render views, controllers do. There is a good reason for the abstraction/separation.)
Helper-style Model processing can be done with Observers that hook in to :before_create/:after_create style callbacks during the ActiveRecord DB storing process.
精彩评论