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!