這是我的第一個導軌應用程序。我的任務是使用.ajax()顯示,刪除和更新記錄。我已經顯示和刪除記錄,但我不能更新記錄,我不知道什麼是錯的。我的控制器代碼是記錄未在導軌中更新
def update
@aj = Aj.find params[:id]
respond_to do |format|
if @aj.update(aj_params)
format.html { redirect_to @aj, notice: 'Aj was successfully updated.' }
format.json { render :show, status: :ok }
else
format.html { render :edit }
format.json { render json: @aj.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_aj
@aj = Aj.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def aj_params
params.require(:aj).permit(:name, :title, :content)
end
end
,我的路由文件是
match '/ajs/' => 'ajs#update', :via => :patch
match '/ajs/:id/edit' => 'ajs#edit', :via => :get
,並可能能夠改變數據後編輯我的表格時,我就更新按鈕,單擊它給出的錯誤
Couldn't find Aj without an ID
# Use callbacks to share common setup or constraints between actions.
def set_aj
@aj = Aj.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
編輯:
我在html本身有javascript代碼
$(document).on("click", ".edit", function(){
var id = $(this).data('id');
alert("edit"+id)
$.ajax({
url: "ajs/"+id+"/edit",
type: "get",
data: id,
//dataType: "html",
success: function(data) {
$("#mybox").html(data);
$(document).on("click", "#subid", function(){
alert(id);
$.ajax({
url: "ajs/"+id,
type: "patch",
success: function(data) {
alert("success");
},
error: function(){
alert("error")
}
}); // show ajax() closed
});//show click() closed
},
error: function(){
alert("error")
}
}); // show ajax() closed
});//show click() closed
我已經添加了更新按鈕的id和id名稱是「subid」,當我點擊那個按鈕時,我通過我的ID獲取該行的id。當我用AJAX傳遞該ID現在我得到另一個錯誤:
No route matches [PATCH] "/ajs"
Rails.root:/家庭/ liplw015 /文檔/導軌/ ajaxjs
在我的控制檯:
Started PATCH "/ajs/134" for 127.0.0.1 at 2014-07-14 09:38:09 +0530
Processing by AjsController#update as */*
Parameters: {"id"=>"134"}
Aj Load (0.4ms) SELECT "ajs".* FROM "ajs" WHERE "ajs"."id" = ? LIMIT 1 [["id", 134]]
Completed 400 Bad Request in 4ms
ActionController::ParameterMissing (param is missing or the value is empty: aj):
app/controllers/ajs_controller.rb:98:in `aj_params'
app/controllers/ajs_controller.rb:69:in `block in update'
app/controllers/ajs_controller.rb:68:in `update'
Rendered /home/liplw015/.rvm/gems/ruby-2.1.2/gems/actionpack- 4.1.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
Rendered /home/liplw015/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.3ms)
Rendered /home/liplw015/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.9ms)
Rendered /home/liplw015/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (22.5ms)
Started PATCH "/ajs" for 127.0.0.1 at 2014-07-14 09:38:10 +0530
ActionController::RoutingError (No route matches [PATCH] "/ajs"):
actionpack (4.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
請出示您的看法d update.js.erb文件。 PS:看起來你使用的是rails 4,但是像rails 3一樣標記了你的問題。 – ryaz