Jos*_*arl 4 ajax jquery ruby-on-rails
在我的Rails应用程序中,我正在尝试使用jQuery ajax通过create我的控制器中的默认方法创建一个新项目.
我routes.rb看起来像这样:
resources :items
Run Code Online (Sandbox Code Playgroud)
服务器端代码仍然是生成的:
# 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
Run Code Online (Sandbox Code Playgroud)
我的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 );
}
});
});
Run Code Online (Sandbox Code Playgroud)
这看起来非常简单,但我收到以下错误:
ActionController::RoutingError (No route matches [POST] "/items/create.json"):
Run Code Online (Sandbox Code Playgroud)
我已经能够update在类似情况下使用默认方法而没有任何问题.这有什么问题?
编辑:修复routes.rb代码中的拼写错误.
控制器中的示例行给出了一个线索.
# POST /items
# POST /items.json
def create
...
Run Code Online (Sandbox Code Playgroud)
create动作只是对/items.json的POST,所以你只需要将你在jQuery中使用的URL改为'/items.json'.
| 归档时间: |
|
| 查看次数: |
1479 次 |
| 最近记录: |