在我的Rails应用程序中,我正在尝试使用jQuery ajax通过我的控制器中的默认create方法创建一个新项目.
我的routes.rb看起来像这样:
resources :items
服务器端代码仍然是生成的:
# POST /items # POST /items.json def create @item = Item.new(params[:item]) respond_to do |format| if @item.save format.html { redirect_to @item, :notice => 'Item was successfully created.' } format.json { render :json => @item, :status => :created, :location => @item } else format.html { render :action => "new" } format.json { render :json => @item.errors, :status => :unprocessable_entity } end end end
我的JavaScript:
$("#capture_input").focusout(function() { var description = $(this).val(); $.ajax({ type: "POST", url: '/items/create.json', data: { item: { description : description } }, dataType: 'json', success: function(msg) { alert( "Data Saved: " + msg ); } }); });
这看起来非常简单,但我收到以下错误:
ActionController::RoutingError (No route matches [POST] "/items/create.json"):
我已经能够在类似情况下使用默认更新方法而没有任何问题.这有什么问题?
编辑:修复routes.rb代码中的拼写错误.
控制器中的示例行给出了一个线索.# POST /items # POST /items.json def create ...
create动作只是对/items.json的POST,所以你只需要将你在jQuery中使用的URL改为’/items.json’.
精彩评论