2

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?

2
  • render returns the html, right? Have you tried { :error => false, :response => "Added", :partial => render(:partial => "partial", :object => @object) } ? Commented Aug 17, 2012 at 15:34
  • 1
    @Robin i am getting an error AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".): Commented Aug 17, 2012 at 15:39

2 Answers 2

2

Solution:

@return = { :error => false, :response => "Added", :partial => render_to_string(:partial => "partial/path", :object => @object) }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.