I'd like to render an action view into a file (or a string it doesn't matter as long as I can write it to a file later), it seems simple enough but I spend many hours on this without success.
Of course we can render a partial or whatever anywhere, but I also need the controller part. So for example, when I go to /mycontroller/myaction I want the rendering to be done into a file (am I making sense ? :)).
Thanks
you're looking for render_to_string method. It accepts all the options you can pass to render but it will return it as a string which you can write to a file.
Use render_to_string as per the Rails rendering guide:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
Related
I have some html that is used a lot in the site i'm building. So I created a App_Code\Helpers.cshtml file and placed the helperfunction in that file.
Now, I want to render a partial-view (a MVC view for a form). But I can't use #Html.Partial("MyFormPartial", new formModel())
I can't find any other ways of rendering a partial view from within a helper class. Anybody got an idea on how to solve this?
Is a seperate helpers.cshtml even the best way for this kind of repeating html-code? I think it gives me a bit more freedom in the parameters I'm providing, instead of the macro's. But it sucks I can't use #Umbraco (without creating your own helper) or #Html :(
Just pass #Html to the helper function if you don't want to create it inside the helper function.
Nevertheless, isn't it a better idea to use a child action and render part of code you'd like to been shared?
How do I export all my rendered view to an html file? I want to save the view to a file rather than display it to the screen. Is this possible?
Instead of render call render_to_string and save the returned string into an file.
(see: http://apidock.com/rails/AbstractController/Rendering/render_to_string)
This sort of sounds like Dynamic Page Caching. There is a Railscasts on this topic. It is rather dated, unless you subscribe (there is a revised version with subscription).
http://railscasts.com/episodes/169-dynamic-page-caching
I'm trying to simplify a view from spree for a tablet, using mobile-fu, I copied the view I want to simplify and named it .tablet.erb and i also had to copy the admin layout and rename it to .tabler.erb, the problem is that the layout uses more partials, and those partials use some more, i'm trying to avoid having to copy all the files just to give them a different name or just plain change the render to tell it to use the html one.
Is there a way to tell it to use html format from that point on? like setting the format recursively?
<% self.formats = [:mobile, :html] %> on each view solved it
Got it from here
My default templating engine is haml, but I would to sometimes change it to erb if i specify a specific parameter?
For example, I am pasting in some html code and would just like to test the code without HAML complaining about its format.
Any idea how to do this?
do something like:
if params[:render_erb]
render 'file.html.erb'
else
render 'file.html.haml'
end
and call the action with ?render_erb=true
or
render "file.html.#{params[:render]}" ir params[:render]
and call it ?render=haml or ?render=erb (or nothing and it will use the default
at the end of the controller's action that you are using
Am I wrong that you simply need to save file as your_file.html.erb instead of your_file.html.haml?
You can use different templates in the same application, and you can use different template engines for views, partials, and layouts, but as far as I know you can't duck in and out of multiple template engines within the same template file.
If you just want to drop some code in using a different template language, then I'd put it in a separate partial. That certainly seems easiest in this particular case.
I am creating a method to generate an XML document via Ruby Builder.
Where should I put the method that created the XML markup? Should it be a method on the model?
I plan to have the XML document pull from multiple models via associations, so I think I need to have it in the controller or a helper, but I would like some in put on the best place.
If it's practical for you, I'd put it in your views folder. This lets you follow the traditional pattern of "load stuff in the controller; render stuff in the view," and has the extra perk of keeping what's probably a messy and very specific method in its own file and out of the way.
Now, I'm not sure if there's a Ruby Builder template format, but you could always just wrap your code in <%= ... %> and treat it like a regular ERB file - should work about the same.
Hope that helps!