0

我在 Rails (3.1.3) 上使用 Ruby (1.9.3-p0) 制作应用程序,但我在使用用于创建对象的远程表单时遇到问题。表单通过远程链接加载到视图中,该远程链接将其放入 div 中(在new操作中。表单也标记为远程,但是当我提交它时,POST 请求尝试由index操作而不是create操作处理。为什么会发生这种情况?Rails 不应该自动使用create控制器的动作来处理基本路由上的 POST 请求吗?以下是相关代码和消息:

这是加载表单的视图中的远程链接:

<div id="new_higher_education_study">
<%= link_to "Nuevo Estudio Superior", new_higher_education_study_path, :remote => true %>
</div>

这是处理所有这些请求的控制器:

class HigherEducationStudiesController < ApplicationController

def new
    @higher_education_study = HigherEducationStudy.new

    respond_to do |format|
        format.js
    end
end

def create
    @study = HigherEducationStudy.new(params[:higher_education_study])
    @higher_education_studies = HigherEducationStudy.get_by_academic_background_id(UserSession.find.user.curriculum_vitae.academic_background_id)

    respond_to do |format|
        if @study.save
            flash[:notice] = "Se ha guardado el Estudio Superior exitosamente."
        else
            flash[:notice] = "Error en el guardado del Estudio Superior."
        end
        format.js
    end
end
end

那么,各自的观点是:

新的.js.erb:

$('#new_higher_education_study').html("<%= escape_javascript(render :partial => 'higher_education_studies/higher_education_study_form' ) %>");

创建.js.erb:

$('#higher_education_studies_table').html("<%= escape_javascript( render :partial => 'higher_education_studies_table') %>")

在我的 routes.rb 文件中,我只为 Rails 设置了该控制器的默认资源,如下所示:resources :higher_education_studies, :except => [:index]

rake routes命令确认路由 POST '/higher_education_studies' 链接到create操作:

               higher_education_studies POST   /higher_education_studies(.:format)                    {:action=>"create", :controller=>"higher_education_studies"}
             new_higher_education_study GET    /higher_education_studies/new(.:format)                {:action=>"new", :controller=>"higher_education_studies"}
            edit_higher_education_study GET    /higher_education_studies/:id/edit(.:format)           {:action=>"edit", :controller=>"higher_education_studies"}
                 higher_education_study GET    /higher_education_studies/:id(.:format)                {:action=>"show", :controller=>"higher_education_studies"}
                                        PUT    /higher_education_studies/:id(.:format)                {:action=>"update", :controller=>"higher_education_studies"}
                                        DELETE /higher_education_studies/:id(.:format)                {:action=>"destroy", :controller=>"higher_education_studies"}

所以我不明白为什么当我提交表单时,我在服务器控制台中得到了这个:

Started POST "/higher_education_studies" for 127.0.0.1 at 2012-04-10 11:12:53 -0300
Processing by HigherEducationStudiesController#index as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"a7kFXPfLvYEBwXurV6rn7apwuAE5p0mGoD5vMaHdcCE=", "higher_education_study"=>{"institution_id"=>"1", "institution"=>"", "degree"=>"law", "major"=>"asdf", "years_studied"=>"6", "status"=>"incomplete"}, "date"=>{"year"=>"2012"}, "commit"=>"Guardar"}
Completed 500 Internal Server Error in 13ms

ActionView::MissingTemplate (Missing template higher_education_studies/index, application/index with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}. Searched in: ...

我尝试将这条线post '/higher_education_studies', :to => 'higher_education_studies#create'明确地放在路线文件上,但这也没有用。我非常感谢你在这个问题上的任何帮助。

4

1 回答 1

1

万一以后有人遇到类似的问题,原来 routes.rb 文件的另一部分正在弄乱这个特定控制器的路由。由于很多人都在做这个项目,也许有人做了一个更通用的路线,在我的控制器的资源之前抓住了我的。我暂时没有调查哪个是罪魁祸首,我只是将它移到resources :higher_education_studies了 routes.rb 文件的几乎顶部,现在它可以工作了。

于 2012-04-11T12:55:02.107 回答