0
我想發佈到嵌套的資源使用$ .ajax後。 不知道我錯過了什麼。感謝你的幫助!
這裏是我的
的routes.rb
resources :clients do
resources :timesheets
end
耙路輸出:
autocomplete_client_name_clients GET /clients/autocomplete_client_name(.:format) clients#autocomplete_client_name
client_timesheets GET /clients/:client_id/timesheets(.:format) timesheets#index
POST /clients/:client_id/timesheets(.:format) timesheets#create
new_client_timesheet GET /clients/:client_id/timesheets/new(.:format) timesheets#new
edit_client_timesheet GET /clients/:client_id/timesheets/:id/edit(.:format) timesheets#edit
client_timesheet GET /clients/:client_id/timesheets/:id(.:format) timesheets#show
PATCH /clients/:client_id/timesheets/:id(.:format) timesheets#update
PUT /clients/:client_id/timesheets/:id(.:format) timesheets#update
DELETE /clients/:client_id/timesheets/:id(.:format) timesheets#destroy
clients GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
edit_client GET /clients/:id/edit(.:format) clients#edit
client GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy
下面是客戶端的jQuery代碼,做後到服務器:
return $("#save_timesheet").click(function() {
return $.ajax({
type: "post",
url: "<%= client_timesheet_url @timesheet.client, @timesheet %>",
data: {
timesheet_data: {
name: "Filip",
description: "whatever",
}
},
success: function(data) {
alert(data.id);
return false;
},
error: function(data) {
return false;
}
});
});
服務器端的URL幫手
<%= client_timesheet_url @timesheet.client, @timesheet %>
了把類似於這樣:
/clients/2/timesheets/44
在服務器端,這導致瞭如下錯誤:
Started POST "/clients/2/timesheets/44" for 127.0.0.1 at 2015-02-08 18:27:58 +0300
ActionController::RoutingError (No route matches [POST] "/clients/2/timesheets/44"):
這裏詳細的服務器日誌:
ActionController::RoutingError (No route matches [POST] "/clients/2/timesheets/44"):
actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
actionpack (4.1.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.0) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.0) lib/rails/engine.rb:514:in `call'
railties (4.1.0) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
「耙路」 ..輸出請 – beck03076 2015-02-08 16:16:45
嘗試運行'''耙routes''',以確保您的路徑匹配其中一條路徑。 – Jake 2015-02-08 16:17:00
也我認爲,<%= client_timesheet_url @timesheet.client%>這應該有助於 – beck03076 2015-02-08 16:17:49