3

我面临一个非常奇怪的路由错误,我似乎无法在 Rails 3.2.11 中解决

错误是:AbstractController::ActionNotFound(找不到 SubmissionsController 的操作“bulk_submissions_url”)

我有一条看起来像这样的路线:

match 'submissions/bulk_submissions_url' => 'submissions#bulk_submissions_url', :as => 'bulk_submissions_url', :via => :post

如下所示的 SubmissionsController:

class SubmissionsController < ApplicationController

...
def bulk_submissions_url
    if signed_in?

      #get the cert that the user has if they are returning to tutorial
      if current_user.enrollments and current_user.enrollments.length > 0
        @enrollment = current_user.enrollments.last
      else
        #handle this
      end  

      @submission = Submission.create!(url: params[:url], description: "Please edit this description", work_type: "other", date_completed: DateTime.now.to_date)  

      if @submission.save
        @enrollment.submissions << @submission
        render :json => { success: true, submission_id: @submission.id }
      else
        render :json => { success: false }
      end

    end
end

我正在使用 jQuery 发出请求:

//save the submission
$.ajax({
type: 'POST',
    url: '<%= bulk_submissions_url_path %>',
    data: {url : $("#tutorial_add_work_url_input").val()},
    dataType: 'json'
});

有什么我想念的吗?

提前致谢!

4

1 回答 1

1

您在名称上使用 _url 是否有原因?这可能会导致混淆,因为名为 my_route 的路由可以通过 my_route_path 和 my_route_url 之类的方法访问,但是如果您将路由命名为 my_route_url,那么它应该由 my_route_url_path 和 my_route_url_url 访问,这是一团糟。

试试这条路线:

match 'submissions/bulk_submissions' => 'submissions#bulk_submissions', :as => 'bulk_submissions', :via => :post

控制器:

class SubmissionsController < ApplicationController

  def bulk_submissions
    if signed_in?

      #get the cert that the user has if they are returning to tutorial
      if current_user.enrollments and current_user.enrollments.length > 0
        @enrollment = current_user.enrollments.last
      else
        #handle this
      end

      @submission = Submission.create!(url: params[:url], description: "Please edit this description", work_type: "other", date_completed: DateTime.now.to_date)

      if @submission.save
        @enrollment.submissions << @submission
        render :json => { success: true, submission_id: @submission.id }
      else
        render :json => { success: false }
      end

    end
  end
end

和阿贾克斯

//save the submission
$.ajax({
type: 'POST',
  url: '<%= bulk_submissions_path %>',
  data: {url : $("#tutorial_add_work_url_input").val()},
  dataType: 'json'
});
于 2013-03-01T00:44:51.070 回答