【问题标题】:Rails Redirect / Post to apply#create via jQuery / AjaxRails Redirect / Post to apply#create via jQuery / Ajax
【发布时间】: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


    【解决方案1】:

    如果出现以下问题

    马克正在申请 37 Signals 的工作。他从 /jobs 开始。 他找到了职位发布:37 正在寻找开发人员。

    他点击并转到 /jobs/37-signals/index,在那里他可以找到工作信息并申请表格。 路线:jobs/12345/index,资源:jobs/:job_id/index

    Job Form Submit 创建一个 POST 请求以 /jobs/12345/create

    如果模型有效,则 2 个选项是:

    1. 使用 Javascript 更新页面。闪存验证。删除了表单并替换为带有从服务器返回的摘要数据并传递给回调成功方法的新 html 模板。

    2. 重定向到摘要控制器并呈现视图。 在 Routes 添加 get 'jobs/:id/summary', to: 'apply#summary' 并添加 Apply#summary 方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多