render_to_string stripping markup - ruby-on-rails

I am using render_to_string within a JSON 'render' response. The render_to_string method seems to be striping my HTML, what am I doing wrong here?
Here's a sample:
render :json => {:html => render_to_string(:partial => 'view', :locals => { data => #data} )}
The response is coming through without any markup on it.

Have you tried setting the template format in the respond_to block like this:
format.json do
#template.template_format = :html
#html = render_to_string( :partial => 'view', :locals => { data => #data} )
render :json => { :success => true, :html => #html }
end

Related

How to have a multi-line return for a function in Rails / Ruby?

The following only renders the last partial line:
def default_sidebars
render :partial => 'shared/sidebars/box1'
render :partial => 'shared/sidebars/box2'
render :partial => 'shared/sidebars/box3'
render :partial => 'shared/sidebars/box4'
end
How could I get it to render all lines?
def default_sidebars
res = render :partial => 'shared/sidebars/box1'
res += render :partial => 'shared/sidebars/box2'
res += render :partial => 'shared/sidebars/box3'
res += render :partial => 'shared/sidebars/box4'
end

What makes the data prepopulate a field if a Form Fails?

I made this form almost entirely from scratch and AJAX which is why it's not working, I'm assuming.
But I instantiate my form in my controller as so..
def skip_to_end
#card_signup ||= CardSignup.new(params[:card_signup])
respond_to do |wants|
wants.html { redirect_to new_card_signup_path }
wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/new_form') } }
end
end
The form looks like this :
- form_for #card_signup do |f|
= f.text_field :first_name, :style => "width: 166px;", :value => "first name", :rel => "first name"
%div{:class => 'error_message'}
....
And when I try and save it halfway complete, it goes to my create action :
def create
#card_signup = current_user.build_card_signup(params[:card_signup])
if #card_signup.valid?
respond_to do |wants|
#wants.html { redirect_to disclaimer_card_signup_path, :locals => { :card_signup => #card_signup } }
wants.json { render :json => { :html => (render_to_string :partial => 'disclaimer') } }
end
else
respond_to do |wants|
#wants.html { redirect_to new_card_signup_path }
wants.json { render :json => {:errors => #card_signup.errors, :html => (render_to_string :partial => '/card_signups/new_form') } }
end
end
end
It then fails, and returns back to the original form, but with all the forms cleared.
How can I make it so that the fields are at the very least prepopulated with what they had in them previously?

Rendering partial with locals in Haml?

I am learning Haml.
My view files are like:
show.html.haml:
.content
= render 'meeting_info', :locals => { :info => #info }
and _meeting_info.html.haml:
.detail
%table
%caption
Meeting Informations of
= info["meeting_name"]
...
When I tried running this I got an undefined local variable or method 'info' error.
Try this
Without :locals and :partial
.content
= render 'meeting_info', :info => #info
No need to specify locals.
With :locals and :partial
You should specify locals in following case i.e specifying :partial for render
.content
= render :partial => 'meeting_info', :locals => { :info => #info }
You would use the :locals option if you're calling render from a controller. When calling render from a view, you would simply do this:
= render 'meeting_info', :info => #info

rails 3 - How to render a PARTIAL as a Json response

I want to do something like this:
class AttachmentsController < ApplicationController
def upload
render :json => { :attachmentPartial => render :partial => 'messages/attachment', :locals => { :message=> #message} }
end
Is there a way to do this? render a Partial inside a JSON object? thanks
This should work:
def upload
render :json => { :attachmentPartial => render_to_string('messages/_attachment', :layout => false, :locals => { :message => #message }) }
end
Notice the render_to_string and the underscore _ in before the name of the partial (because render_to_string doesn't expect a partial, hence the :layout => false too).
UPDATE
If you want to render html inside a json request for example, I suggest you add something like this in application_helper.rb:
# execute a block with a different format (ex: an html partial while in an ajax request)
def with_format(format, &block)
old_formats = formats
self.formats = [format]
block.call
self.formats = old_formats
nil
end
Then you can just do this in your method:
def upload
with_format :html do
#html_content = render_to_string partial: 'messages/_attachment', :locals => { :message => #message }
end
render :json => { :attachmentPartial => #html_content }
end
This question is a bit old, but I thought this might help some folks.
To render an html partial in a json response, you don't actually need the with_format helper as explained in mbillard's answer. You simply need to specify the format in the call to render_to_string, like formats: :html.
def upload
render json: {
attachmentPartial:
render_to_string(
partial: 'messages/attachment',
formats: :html,
layout: false,
locals: { message: #message }
)
}
end
In Rails 6 I think this might be a little different from the accepted answer. I don't think you need to set the underscore in the partial name. This worked for me:
format.json {
html_content = render_to_string(partial: 'admin/pages/content', locals: { page: #page }, layout: false, formats: [:html])
render json: { attachmentPartial: html_content }
}

Ajax and vote_fu | missing before statement

relate question: Vote_fu and Ajax requests
There seems to be something wrong with my Ajax request.
What I 'm trying to do is on event click vote submit a vote then update page with out refreshing the page.
votes_controller.rb:
def create
#album = Album.find(params[:album_id])
respond_to do |format|
if current_user.vote(#album, params[:vote])
format.js { render :action => "create", :vote => #vote }
format.html { redirect_to([#album.user, #album]) }
#format.xml { render :xml => #album, :status => :created, :location => #album }
else
format.js { render :action => "error" }
format.html { render :action => "new" }
format.xml { render :xml => #vote.errors, :status => :unprocessable_entity }
end
end
end
link | for view album show :
<%= link_to_remote "Vote Up",
:url => user_album_votes_path(album.user, album,
:vote => :true, :format => :js),
:method => :post %>
application.js
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
jQuery(document).ready(function() {
$("#votes_.album").bind('click');
})
create.js
page.replace_html "#votes_{#album.id}",
:partial => "album_vote",
:locals => {:album => #album}
This is the following error message which I'm getting:
missing ; before statement
[Break on this error] page.replace_html "#votes_#{#album.id}", ...bum_vote", :locals => {:album => #album}
I'm not sure what is going wrong here I have been following many example from the vote_fu doc's
still having problems.
http://github.com/peteonrails/vote_fu/tree#readme
one amendment made on create.js
there is now another error:
No the error has moved over to the votes_controller
NoMethodError (You have a nil object when you didn't expect it!
<br />
The error occurred while evaluating nil.vote):
app/controllers/votes_controller.rb:53:in `create'
app/controllers/votes_controller.rb:52:in `create'
<br />
Rendered rescues/_trace (128.4ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)
These lines are on the create action, which looks perfectly fine!?
How do I get this to work?
Regard
Dan
Try changing the create.js to
page.replace_html "#votes_#{#album.id}", :partial => "album_vote", :locals => {:album => #album}
You might have missed the # for the variable string interpolation.
Solved!
The problem was that I didn't add a before statement to refresh the vote count!
so I did and it worked as well as that, I changing the create.js to create.sj.erb, also I made some small changes to my application.js file. After all that I then added a flash[:note] = you have voted!, then added a function to remove the flash notice after a sec and fadeOut!
For anyone who's interested heres the code:
Application.js
jQuery(document).ready(function() {
$("#vote").bind('click', function(e) {
if (e.target.tagName == "DIV") {
$(this).find(".album_extened").toggle('blind');
}
})
})
create.js.erb
$("#vote").before('<div id="notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#vote_count").html("<%= #album.votes_for - #album.votes_against %>");
$(document).ready(function() {
setTimeout(hideFlashMessages, 1000);
});
function hideFlashMessages() {
$("#vote, #notice").append('').fadeOut(1000);
}
If anyone knows a better why which this can be done please forward!!
A good viewing of [http://railscasts.com/episodes/136-jquery][1]
Reading of [link text][2]
Soon fixed things Thanks Jonathanlink text
[1]: http://railscasts.com/episodes/136-jquery/"Rails Cast ep 136"
[2]: http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/"Beginners Guild to jQuery and Rails"
and Thanks Sam for being helpful!!
It seems to me that the current_user in the create method did not get its value set.
How do you set the current_user value? In other plugins, they usually define this as an
instance method or instance variable with accessor.
So maybe changing the create method to the following might help:
def create
#album = Album.find(params[:album_id])
respond_to do |format|
if #current_user.vote(#album, params[:vote])
format.js { render :action => "create", :vote => #vote }
format.html { redirect_to([#album.user, #album]) }
#format.xml { render :xml => #album, :status => :created, :location => #album }
else
format.js { render :action => "error" }
format.html { render :action => "new" }
format.xml { render :xml => #vote.errors, :status => :unprocessable_entity }
end
end
end
You might have missed the # before the current_user.vote

Resources