我正在使用Rails(3.1.3)在Ruby(1.9.3-p0)上创建一个应用程序,并且我遇到了一个用于创建对象的远程表单的问题。表单通过远程链接加载到视图中,该链接将其放入div中(在new
操作中。表单也标记为远程,但是当我提交它时,POST请求尝试由{处理} {1}}操作而不是index
操作。为什么会发生这种情况?Rails是否应该自动使用控制器的create
操作来获取基本路由上的POST请求?这是相关代码和消息:
以下是视图中的远程链接,用于加载表单:
create
以下是处理所有这些请求的控制器:
<div id="new_higher_education_study">
<%= link_to "Nuevo Estudio Superior", new_higher_education_study_path, :remote => true %>
</div>
然后,各自的观点是:
new.js.erb:
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
create.js.erb:
$('#new_higher_education_study').html("<%= escape_javascript(render :partial => 'higher_education_studies/higher_education_study_form' ) %>");
在我的routes.rb文件中,我只使用该控制器的默认资源设置Rails,如下所示:$('#higher_education_studies_table').html("<%= escape_javascript( render :partial => 'higher_education_studies_table') %>")
resources :higher_education_studies, :except => [:index]
命令确认路由POST'/ higher_education_studies'已链接到rake routes
操作:
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: ...
明确地放在路径文件上,但这也行不通。我非常感谢你对这个问题的任何帮助。
答案 0 :(得分:1)
如果某人将来遇到类似的问题,那么看来routes.rb文件的另一部分正在弄乱这个特定控制器的路由。由于许多人正在从事该项目,也许有人在我的控制器资源之前制定了一条更为通用的路线。我现在没有看到哪个是罪魁祸首,我只是将resources :higher_education_studies
移动到几乎是routes.rb文件的顶部,现在它可以正常工作。