Question

I'm creating my users and their info via Ajax post on the view apply/index with the controller apply (def create) via rails.

Routes:

  resources :jobs, :only => [:show, :index, :create] do
    resources :apply, :only => [:index, :create]
    resources :share, :only => [:index, :create]
  end

Rake routes:

job_apply_index:
GET    /jobs/:job_id/apply(.:format)                apply#index
POST   /jobs/:job_id/apply(.:format)                apply#create

Situation before my changes: when I was not using Ajax to Post but using a rails post. If I filled in my form correctly the form redirect to the views/apply/create page.

Current Situation: Form:

<%= form_tag job_apply_index_path(@job.sap_id), :class => "apply", :multipart => true, :remote => true, :name => "ajaxform", :id => "ajaxform" do %>

apply.js.Coffee:

  $("#register_button").on 'click', (e) ->
    e.preventDefault()
    $('#ajaxform').submit()

  $("#ajaxform").submit (e)->
    e.preventDefault()
    postData = $(this).serializeArray()
    formURL = $(this).attr("action")
    $.ajax
      url: formURL
      type: "POST"
      data: postData
      success: (data, textStatus, jqXHR) ->        
        $("#loadingSpinner").fadeOut "slow" 
        handle_messages data 
      error: (jqXHR, textStatus, errorThrown) ->
        #todo

handle_message:

handle_messages = (msg_list) ->
  console.log msg_list
  $("#errorTitle").empty()
  $("#errorList").empty()
  if msg_list.errors
    #TODO
  else if msg_list.notice
    #TODO
  else if msg_list.redirect
    window.location.replace msg_list.redirect_url
  else 
    alert "not good son"

My response in apply_controller:

# Respond with error messages
if @user.errors.full_messages.any? || @user.personal_info.errors.full_messages.any?
  errors_msg =  @user.errors.full_messages + @user.personal_info.errors.full_messages
  errors_msg.map! {|error| {message: error}}
  msg = {:errors => errors_msg, :title => I18n.t('validation_header', :errors => errors_msg.count)}
elsif notice_msg
  msg = {:notice => notice_msg}
else
  msg = {:redirect => true, :redirect_url => "path_to_job_create"}
end

# Pass redirect params
respond_to do |format|
  format.json  { render :json => msg }
  format.html { render action: "index" }
  format.js
end

Goal: I want to redirect to the views/apply/create after the user has filled in everything correctly. The same as before I was using ajax.

Need any more information? Just ask!

Était-ce utile?

La solution

In the case of the below problem

Mark is applying for a job at 37 Signals. He starts at /jobs. He find the job posting: 37 looking for a dev.

He clicks and go to /jobs/37-signals/index where he finds job info and apply form. route: jobs/12345/index, resource: jobs/:job_id/index

Job Form Submit creates a POST request to /jobs/12345/create

If the model is valid the 2 options are:

  1. Use Javascript to update page. Flash Verifications. Removed the form and replace with with a new html template with summary data returned from the server and passed to the callback success method.

  2. Redirect to a summary controller and render the view. In Routes add get 'jobs/:id/summary', to: 'apply#summary' and add Apply#summary method

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top