Rails重定向/发布应用#通过jQuery/Ajax创建

Rails重定向/发布应用#通过jQuery/Ajax创建,jquery,ruby-on-rails,ajax,redirect,Jquery,Ruby On Rails,Ajax,Redirect,我通过rails使用controller apply(def create)在视图apply/index上通过Ajax帖子创建我的用户及其信息 路线: resources :jobs, :only => [:show, :index, :create] do resources :apply, :only => [:index, :create] resources :share, :only => [:index, :create] end job_

我通过rails使用controller apply(def create)在视图apply/index上通过Ajax帖子创建我的用户及其信息

路线:

  resources :jobs, :only => [:show, :index, :create] do
    resources :apply, :only => [:index, :create]
    resources :share, :only => [:index, :create]
  end
job_apply_index:
GET    /jobs/:job_id/apply(.:format)                apply#index
POST   /jobs/:job_id/apply(.:format)                apply#create
# 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
Rake路由:

  resources :jobs, :only => [:show, :index, :create] do
    resources :apply, :only => [:index, :create]
    resources :share, :only => [:index, :create]
  end
job_apply_index:
GET    /jobs/:job_id/apply(.:format)                apply#index
POST   /jobs/:job_id/apply(.:format)                apply#create
# 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发布,而是使用rails发布时。 如果我正确填写表格,表格将重定向到查看/应用/创建页面

当前情况: 表格:

处理信息:

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"
我在应用控制器中的响应:

  resources :jobs, :only => [:show, :index, :create] do
    resources :apply, :only => [:index, :create]
    resources :share, :only => [:index, :create]
  end
job_apply_index:
GET    /jobs/:job_id/apply(.:format)                apply#index
POST   /jobs/:job_id/apply(.:format)                apply#create
# 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


需要更多的信息吗?问吧

在以下问题的情况下

马克正在申请一份37号信号公司的工作。他从工作开始。 他找到了那个职位:37岁,正在寻找一名开发人员

他点击并进入/jobs/37 signals/index,在那里他可以找到工作信息并申请表格。 路由:作业/12345/索引,资源:作业/:作业id/索引

工作表单提交创建一个POST请求 /jobs/12345/create

如果模型有效,则两个选项为:

  • 使用Javascript更新页面。闪存验证。删除表单并替换为新的html模板,其中包含从服务器返回并传递给回调成功方法的摘要数据

  • 重定向到摘要控制器并呈现视图。 在路由中,将get'jobs/:id/summary'添加到:'apply#summary' 并添加应用#摘要方法