我在Ruby(1.9.3-p0)上在Rails(3.1.3)上創建了一個應用程序,並且遇到了遠程形式,我用來創建一個對象。表單通過遠程鏈接加載到視圖中,該鏈接將其放入div(在new
操作中。表單也標記爲遠程,但是當我提交它時,POST請求會嘗試由index
操作處理,而不是create
爲什麼會發生這種情況?不應該使用控制器的create
動作自動使用基本路由上的POST請求嗎?這裏是相關的代碼和消息:Rails 3:通過索引操作而不是創建遠程(UJS)表單
這是遠程鏈接的視圖加載形式:
<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
然後,各自的觀點是:
new.js.erb:
$('#new_higher_education_study').html("<%= escape_javascript(render :partial => 'higher_education_studies/higher_education_study_form') %>");
create.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'
明確地放在路由文件上,但那也沒有奏效。我非常感謝你對這個問題的任何幫助。