Topic: how to render a rjs template if previous request is none-ajax?

hello,

I used a formtastic form (semantic_form_for) to create a new record.
I have a modal popup over a list of records. In the popup there're 2 buttons "Save" and "Save & Add another". If user clicks on "Save", I will:
- hide the form
- create new record (in controller, model)
- refresh the list

if user clicks on "Save & Add another",  I want to:
- create a new record
- display the form with all fields cleared.

the problem is, in controller, after creating a new record (by calling a model method), i use:

respond_to do |format|
  format.js {render :template => "save_and_clear_form"}
end

also, i have a "save_and_clear_form.js.rjs" in /views/....

but it keeps printing out the content of the .rjs file in the web browser!

How to fix this?

btw, are .rjs files only used when the previous request is xhr?

Thanks.

Last edited by ndgiang84 (2010-11-09 04:48:49)

Giang, Nguyen Duc
Email: ndgiang84@gmail.com
Website: http://xiga.info
Please recommend for me on WorkingWithRails if you find my post useful: http://workingwithrails.com/person/1849 … -duc-giang

Re: how to render a rjs template if previous request is none-ajax?

if request.xhr?
  #do your stuf if it's an Ajax request
else
 #do your stuff if it's a HTTP request
end

Re: how to render a rjs template if previous request is none-ajax?

Cool, thanks for such a concise, terse reply.

Any recommendation of how to implement my flow?

Last edited by ndgiang84 (2010-11-09 14:32:33)

Giang, Nguyen Duc
Email: ndgiang84@gmail.com
Website: http://xiga.info
Please recommend for me on WorkingWithRails if you find my post useful: http://workingwithrails.com/person/1849 … -duc-giang