render_to_string is deprecated but there is no replacement mentioned in the API docs. How can you render a partial to a string in a controller without using this function? I need this to do things like this:
render :update do |page|
...
page.call "Lightbox.create", render_to_string(:partial => "...", ...)
end
Or:
render :json => {
...,
:message => render_to_string(:partial => "...", ...)
}
render :partial => "..." should return the rendered partial (unlike render "a_view").
render_to_string method has been moved to the ActionController::Rendering module.
Here is the render_to_string source code
Try to call render_to_string on ActionController::Base like so:
ActionController::Base.new.render_to_string ...
Related
I have a old rails 2.x project that I have converted mostly over to rails 5.
One issue is some of my actions used RJS, so it looked like:
if request.xhr?
render :action => 'new_user' and return
end
The new_user.js.rjs looks something like:
page.call "User.create", render(:partial => 'new_user'), {:userId => #user.id}
Looking at the response in chrome I can see it is just returning:
User.create('<tr><td>....</td></tr>', {"userId" : 123});
I only have to support the page.call type RJS call, what would be a easy "hack" to get this to work in rails 5?
I don't want to modify all of my javascript code, I just need to basically have a javascript block that I pass the JS code to in my view pages right?
Try to render a response as a string:
if request.xhr?
render render_to_string partial: 'new_user',
locals: { userId: #user.id },
layout: false
end
Or try to use format handler instead:
respond_to do |format|
format.rjs do
render render_to_string partial: 'new_user',
locals: { userId: #user.id },
layout: false
end
end
I ended up returning a JSON response to my view pages like this:
some_partial_html = render_to_string(:partial => 'something')
response = {
"html": some_partial_html,
"a" : 1
}.to_json
render json: response
And then in my view I used the json response values as arguements to the javascript object that performs the functionality I needed.
How to get the xml output of a view/controller as string within the same controler
This is the routes file
routes.rb
map.ccda '/ccda/ccda_patient_search', :controller => 'ccda', :action => :ccda_patient_search
map.ccda '/ccda/:id.:format', :controller => 'ccda', :action => :index
ccda_controller.rb
class CcdaController < ApplicationController
def index
#
# some process
# result = User.find(params[:id]).result
#
#ccda = result
respond_to do |format|
format.xml { render :layout => false, :template=> "index.xml.builder" }
format.any { redirect_to login_url }
end
end
def get_xml
# render_to_string 'index', :layout=>false, :formats=>[:xml], :locals=>{:id=>147} => Not working
# render_to_string '147.xml' => Not working
#
# How do I get the output of 'http://localhost/ccda/147.xml' here???
#
end
end
I will use the url localhost/ccda/147.xml to view/generate the users result as xml
Now I want the output of that url as a string without returning to browser
I've tried to get it from same controller using render_to_string method with diffrent parameters but nothing seems to work
FYI: I am using rails 2.3.12 and Builder::XmlMarkup API
How about using the following call (inside controller, have taken the known options from your question):
render_to_string(:template => 'ccda/index.xml.builder', :layout => false, :id => 147)
Due to the documentation, this will work up to Rails version 2.3.8, so I don't know if it is available any more in Rails 2.3.12 you are using.
PS: How about upgrading to at least last version of Rails 3? I don't have a change to test my solution, so it is guesswork more or less.
How about this below?
render_to_string :controller => 'ccda_controller', :action => 'index', :id => 147, :format => :xml
Finally I've found that we need to specify the view file manually because by default rails will be looking for index.erb so what I've done is this
render_to_string( :action=>"index", :view => "/ccda/index.xml.builder", :format=>:xml,:layout=>false,:id=>146, :template=>"/ccda/index.xml.builder" )
specifying :view and :template manually solved my problem
I am trying to render a partial for the ajax request but i can't find how to do it using Ruby on Rails...
here what my array variable
#return = { :error => false, :response => "Added", :partial => ... }
render :json => ActiveSupport::JSON.encode( #return )
where ... is where the partial(html) should be...
the method name is add_item, its controller is items and i have created an add_item.html.erb file inside the items folder which has the HTML i want to pass to the array and use jQuery to add it to the DOM
i guess this could be done using
render :partial => "partial", :object => #object
but how can i add this into the array above?
Solution:
#return = { :error => false, :response => "Added", :partial => render_to_string(:partial => "partial/path", :object => #object) }
The way I do what I think you're trying to do is to fire an ajax message at add_item, and then in add_item have:
def add_item
respond_to do |format|
format.js
end
end
and make an add_item.js.erb with the jQuery contents:
$('#div-for-code').html('<%= escape_javascript(render :partial => "mypartial") %>');
not sure that it's the best way but it's worked for me.
What used to work with Rails 3.1.0 was:
class MyController < ApplicationController
...
def myAction
...
data = render_to_string( :template => "reports/report.xml.builder", :layout => false)
...
end
end
With Rails 3.2 I am getting a deprecation warning. How do I render an arbitrary xml builder view with Rails 3.2?
Instead of
render(:template => "reports/report.xml.builder", :layout => false)
Rails 3.2 wants
render(:template => “reports/report”, :formats => [:xml], :handlers => :builder, :layout => false)
In my case I've '_test.xm.builder' partail so I have to use render_to_string it work like this
render_to_string(partial: "/assignments/test", :formats => [:xml], :locals=> {:abc=>'xyz'})
this will cure :)
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.