【发布时间】:2014-05-15 18:30:02
【问题描述】:
我正在通过 Ajax 帖子在视图应用/索引上创建我的用户及其信息,控制器通过 rails 应用(def create)。
路线:
resources :jobs, :only => [:show, :index, :create] do
resources :apply, :only => [:index, :create]
resources :share, :only => [:index, :create]
end
Rake 路线:
job_apply_index:
GET /jobs/:job_id/apply(.:format) apply#index
POST /jobs/:job_id/apply(.:format) apply#create
我更改前的情况:当我不使用 Ajax 发布但使用 Rails 发布时。 如果我正确填写表单,表单将重定向到 views/apply/create 页面。
现状: 表格:
<%= 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"
我在 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
目标: 在用户正确填写所有内容后,我想重定向到视图/应用/创建。和我之前使用ajax一样。
需要更多信息吗?只要问!
【问题讨论】:
标签: jquery ruby-on-rails ajax redirect