记录未在rails中更新

时间:2014-07-11 04:36:40

标签: ruby-on-rails-3 ruby-on-rails-4

这是我的第一个rails应用程序。我的任务是使用.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:/ home / liplw015 / Documents / rails / 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'

1 个答案:

答案 0 :(得分:0)

您的路线错误,包含路线中的ID

match '/ajs/:id' => 'ajs#update', :via => :patch