I want to get the rendered response from the action BEFORE it returns, so something like:
def test
my_html = # RENDER VIEW HERE AND ASSIGN TO VARIABLE
render :text => my_html
end
How can I do this?
You could use render_to_string
def test
my_html = render_to_string(:action => :show)
render :text => my_html
end
render_to_string accepts all options that render does.
Related
I am basically trying to use a helper method that renders a partial in my controller and getting a nocontent errors or 500 errors "no template" depending on the variations I'm trying. If I just run the code in the helper directly from the controller action, all is well. It seems like the rails guides don't touch on this (using a helper method that does the rendering, as opposed to directly from the controller action). Unfortunately, I don't have the luxury right now to refactor it out because the helper is used in other places, and it seems to me if there is a way to use this helper in the controller action also, that's the better option.
Anyone know how to do this? Nothing I am trying is working for me :S
Helper method
def render_call_orders_filters url_or_path_method, query_params = #query_params
render :partial => 'call_orders/call_orders_filters', :locals => {url_or_path_method: url_or_path_method, query_params: query_params}
end
Controller action
# GET /call_order/filters
def filters
respond_to do |format|
format.html {
# This (of course) works...
# render :partial => 'call_orders/call_orders_filters', \
# :locals => { url_or_path_method: method(:call_orders_path), query_params: #query_params }
# This does not
helpers.render_call_orders_filters(method(:call_orders_path))
# This gives me a "Template is missing" 500 error
renter text: helpers.render_call_orders_filters(method(:call_orders_path))
}
end
end
based on your last example code, there is render :partial => that works
I think it could be refactored like this
# The helper...
def orders_filters_param_to_template(url_or_path_method, query_params = {})
{
partial: 'call_orders/call_orders_filters', locals: { url_or_path_method: url_or_path_method,
query_params: query_params }
}
end
def render_call_orders_filters(url_or_path_method, query_params = #query_params)
template_data = orders_filters_param_to_template(url_or_path_method, query_params)
render partial: template_data[:partial],
locals: template_data[:locals]
end
# The controller action...
def filters
respond_to do |format|
format.html do
template_data = helpers.orders_filters_param_to_template(method(:call_orders_path), #query_params)
render partial: template_data[:partial],
locals: template_data[:locals]
end
end
end
I am curious if this is even possible. I am looking to render data based on the results of an action in ruby on rails. Here is the code:
def convert
render :text => Item.convert(params[:file], params[:output])
end
As you can see currently it just renders text. The item model takes in two parameters: a file and output. The output could be xml, json, plain text, etc. Some type of data. What I was hoping I could do is that I could render the output based on the output parameter. But make it dynamic. I was hoping I could do:
def convert
#items = Item.convert(params[:file], params[:output])
respond_to do |format|
format.json { render :json => #items }
format.xml { render :xml => #items }
end
end
Something that would call the method and based on the data returned it would render the appropriate data. Testing the above code returns an empty page. The body tag is completely empty.
Any ideas?
thanks
Mike Riley
If you are relying on the name of params[:output] to determine what to render, I would write is as below:
def convert
#items = Item.convert(params[:file], params[:output])
if File.extname(params[:output].match("text"))
render :text => Item.convert(params[:file], params[:output])
elsif File.extname(params[:output].match("json"))
render :json => Item.convert(params[:file], params[:output])
elsif File.extname(params[:output].match("xml"))
render :xml => Item.convert(params[:file], params[:output])
else
render :html => "default_html"
end
end
and you should have a default_html.html.erb that you would render if the extension name of the file does not match what is expected
In a controller method, how can you render a template and layout?
Like so:
def new
render :template => 'devise/invitations/new', :layout => 'application_unauthorized2_t2'
end
Instead of just being an options hash, like most rails methods, the render method is a series of arguments, the last of which is an options hash. The first argument to render is the template, as a string. You don't need to include it in the options hash.
Just do this:
def new
render 'devise/invitations/new', :layout => 'application_unauthorized2_t2'
end
Using a Class level layout method
One of my model objects has a 'text' column that contains the full HTML of a web page.
I'd like to write a controller action that simply returns this HTML directly from the controller rather than passing it through the .erb templates like the rest of the actions on the controller.
My first thought was to pull this action into a new controller and make a custom .erb template with an empty layout, and just <%= modelObject.htmlContent %> in the template - but I wondered if there were a better way to do this in Rails.
In your controller respond_to block, you can use:
render :text => #model_object.html_content
or:
render :inline => "<%= #model_object.html_content %>"
So, something like:
def show
#model_object = ModelObject.find(params[:id])
respond_to do |format|
format.html { render :text => #model_object.html_content }
end
end
In latest Rails (4.1.x), at least, this is much simpler than the accepted answer:
def show
render html: '<div>html goes here</div>'.html_safe
end
Its works for me
def show
#model_object = ModelObject.find(params[:id])
respond_to do |format|
format.html { render :inline => "<%== #model_object['html'] %>" }
end
end
I want to explicitly call a view from my controller.
Right now I have:
def some_action
.. do something ...
respond_to do |format|
format.xml
end
end
... then it calls my some_action.xml.builder view. How can I call some other view? Is there a parameter in respond_to I'm missing?
Thanks,
JP
You could do something like the following using render:
respond_to do |format|
format.html { render :template => "weblog/show" }
end
See the Rendering section of the ActionController::Base documentation for the different ways you can control what to render.
You can tell Rails to render a specific view (template) like this:
# Renders the template located in [TEMPLATE_ROOT]/weblog/show.r(html|xml) (in Rails, app/views/weblog/show.erb)
render :template => "weblog/show"
# Renders the template with a local variable
render :template => "weblog/show", :locals => {:customer => Customer.new}
Or even simpler since Rails > 3.0:
render "edit"
You can also pass :action, or :controller if that's more convenient.
respond_to do |format|
format.html { render :action => 'show' }
end
You can modify the internal lookup_context of the controller by doing this in your controller
before_filter do
lookup_context.prefixes << 'view_prefix'
end
and the controller will try to load view/view_prefix/show.html when responding to an show request after looking for all the other view prefixes in the list. The default list is typically application and the name of the current controller.
class MagicController
before_filter do
lookup_context.prefixes << 'secondary'
end
def show
# ...
end
end
app.get '/magic/1`
This GET request will look for a view in the following order:
view/application/show.erb
view/magic/show.erb
view/secondary/show.erb
and use the first found view.
Use render
http://api.rubyonrails.com/classes/ActionController/Base.html#M000474