ruby on rails 3 - Controller can not detect ajax requests -


i using simple_form gem , generating form specifying remote:true option this:

<%= simple_form_for @webinar, validate: true, remote:true |f| %> 

so, output html form following fragment:

<form accept-charset="utf-8" action="/webinars" class="simple_form new_webinar" data-remote="true" data-validate="true" enctype="multipart/form-data" id="new_webinar" method="post" novalidate="novalidate"> ... </form> 

as checked, using standard form_for helper adding data-remote='true' form when remote:true options used. , can see generated html, when using simple_form gem there such attribute, too.

so, in controller have:

def create   @webinar = webinar.new(params[:webinar])    respond_to |format|     if @webinar.save       format.html { redirect_to @webinar, notice: 'webinar created.' }       format.js       format.json { render json: @webinar, status: :created, location: @webinar }     else       format.html { render action: "new" }       format.json { render json: @webinar.errors, status: :unprocessable_entity }     end   end end 

but, format.html used. doing wrong?

edit:

i have used logger.debug request.format check actual format ask , in log file was:

text/html

so, issue must in simple_form generated form - can wrong there when have "data-remote=true"?

you're confusing format.json|format.html remote: true. both different. presence of remote: true not imply format.json.

format.json does not indicate url invoked via javascript. means client expects json output. i.e. not indicate input came from, indicates output required.

the general use of remote:true is, instead of reloading page, submit form ajax request , show response in jquery popup or something. if want display response jquery popup - need html output, not json output right?

some people use remote: true load html content in popup. use case remote: true you're expecting json formatted data. rails cannot make these decisions you. default sends request /webinars , expects handle html response. if want json response - customize url ajax request posted:

simple_form_for @webinar, url: webinar_path(format: :json), ..... 

if above, webinar controller called json format.

overall:

  • remote:true can used both format.html , format.json
  • the majority use case in rails application handle remote: true controller request usual, render partial html template (i.e. response content alone without overall page layout/navigation/etc) , send html displayed in popup
  • most people blanket-handle remote callbacks , display jquery popup. don't need write individual code each remote forms
  • so default, rails calls format.html remote requests
  • if want format.json, , if want handle json manually on client, change url accordingly. not majority use case
  • making ajax request does not mean content-type json. html request made using javascript. e.g. in jquery $.ajax method, check these 2 options: accept , datatype. if want send accepts: application/json header, have manually specify when making ajax request (or need end url .json if rails app).
  • so if make normal ajax request /webinars, $.ajax('/webinars', ...) - won't go format.json ! still go format.html. if want json format, must $.ajax('/webinars', { accepts: 'application/json' }), or must $.ajax('/webinars.json')

edit: minor clarification


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -