我知道這個問題是在網絡上貼滿了,但看着所有的答案後,我仍然無法弄清楚我的問題是什麼。Rails的未初始化的常量路由錯誤
這裏是我的路線:
resources :applications do
resources :applicants do
collection {put '/archived'}
collection {put '/not_archived'}
end
end
然後我有一個表,在此路徑:/applications/1/applicants
這就要求我的索引方法。
在此表中列出了所有的申請者,我有各行的複選框,當他們選中該複選框,我想設置的「歸檔」標誌的分貝使用AJAX該行。
這裏是我的Ajax調用:
$('.archive').on('click', function() {
if ($(this).is(':checked')) {
$.ajax({
type: "PUT",
url: '/applications/<%= @application.id %>/applicants/archived',
data: {id: $(this).attr('id'), archived: true}
});
} else {
$.ajax({
type: "PUT",
url: '/applications/<%= @application.id %>/applicants/not_archived',
data: {id: $(this).attr('id'), archived: false}
});
}
});
但每次我選中一個複選框,我得到這個錯誤:
Started PUT "/applications/1/applicants/archived" for 127.0.0.1 at 2013-04-03 00:07:20 -0500
ActionController::RoutingError (uninitialized constant Applications):
activesupport (3.2.6) lib/active_support/inflector/methods.rb:229:in `block in constantize'
activesupport (3.2.6) lib/active_support/inflector/methods.rb:228:in `each'
activesupport (3.2.6) lib/active_support/inflector/methods.rb:228:in `constantize'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:69:in `controller_reference'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:54:in `controller'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:32:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.6) lib/action_dispatch/routing/route_set.rb:600:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.6) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `_run__281193607366851886__call__1094349027316042218__callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.6) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
quiet_assets (1.0.1) lib/quiet_assets.rb:20:in `call_with_quiet_assets'
actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
webrick (1.3.1) lib/webrick/httpserver.rb:138:in `service'
webrick (1.3.1) lib/webrick/httpserver.rb:94:in `run'
webrick (1.3.1) lib/webrick/server.rb:191:in `block in start_thread'
我ApplicantsController有以下方法:
# PUT /applicants/archived/1
def archived
p 'here?'
@applicant = Applicant.find(params[:id])
@applicant.archived = params[:archived]
#@application = current_company.applications.find(params[:application_id])
respond_to do |format|
if @applicant.save
format.js
#format.html { redirect_to @task, notice: 'Task was successfully updated.' }
#format.json { head :no_content }
else
format.html { render action: "index" }
format.json { render json: @applicant.errors, status: :unprocessable_entity }
end
end
end
# PUT /applicants/not_archived/1
def not_archived
@applicant = Applicant.find(params[:id])
@applicant.archived = params[:archived]
#@application = current_company.applications.find(params[:application_id])
respond_to do |format|
if @applicant.save
format.js
#format.html { redirect_to @task, notice: 'Task was successfully updated.' }
#format.json { head :no_content }
else
format.html { render action: "index" }
format.json { render json: @applicant.errors, status: :unprocessable_entity }
end
end
end
這裏是我的耙線路輸出這些路線:
archived_application_applicants PUT /applications/:application_id/applicants/archived(.:format) applications/:application_id/applicants#archived
not_archived_application_applicants PUT /applications/:application_id/applicants/not_archived(.:format) applications/:application_id/applicants#not_archived
application_applicants GET /applications/:application_id/applicants(.:format) applicants#index
POST /applications/:application_id/applicants(.:format) applicants#create
new_application_applicant GET /applications/:application_id/applicants/new(.:format) applicants#new
edit_application_applicant GET /applications/:application_id/applicants/:id/edit(.:format) applicants#edit
application_applicant GET /applications/:application_id/applicants/:id(.:format) applicants#show
PUT /applications/:application_id/applicants/:id(.:format) applicants#update
DELETE /applications/:application_id/applicants/:id(.:format) applicants#destroy
什麼引起我的錯誤?我完全被難住了。
在控制器PUT /申請/存檔/ 1,看來你需要創建歸檔爲成員的路線不收集。然後,你需要傳遞你的ajax調用的id。而且你還需要重新配置你的ajax路徑。 – Mohanraj
您是否爲申請和申請人使用一個控制器?或者有2個控制器?他們在哪裏放置?看起來像名稱空間問題 – Abid