I have this controller method:
def create
render plain: params[:article].inspect
#@article = Article.new(article_params)
#@article = Article.create(article_params)
#if @article.save
# redirect_to @article
#else
# render 'new'
#end
end
And this jquery ajax request:
function ajaxsend() {
$.ajax({
url: "/articles",
type: "POST",
data: { "article": { "title": "acme", "text": "text of the article" } },
success: function(resp){ }
});
}
But after I click the button that run e jquery ajax I get nothing. I expect the rendering but nothing happens, not even a error in the console. What am I doing wrong? I don't understand how can I send data thought jquery ajax to the controller. Any help? I'm new in ruby on rails development. Ruby version: 2.1.2; Rails version: 4.1.5;
Edit1:
It works well, when I do it in the traditional way in the form:

Result:

Edit2:
The WEBrick server prints this, when I press the button:
Started POST "/articles" for 127.0.0.1 at 2014-09-14 09:43:36 +0100
Processing by ArticlesController#create as */*
Parameters: {"article"=>{"title"=>"acme", "text"=>"123 Carrot Street"}}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
It seems ok. Why does nothing happens in the browser? Why the render in create method is not working? Any help?
The WEBrick response when I fill the form and press Create Article:
Started POST "/articles" for 127.0.0.1 at 2014-09-14 09:52:21 +0100
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"o8bzaGFcWGaHFFEbgZsfx5bWiHDAIaX3j4xXh3XkTwc=", "article"=>{"title"=>"mmmmm", "text"=>"mmmmm"}, "commit"=>"Create Article"}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
Edit3:
I also tried this way:
$.post("/articles", { "article": { "title": "Title1Ar", "text": "text of the article" } }, alert("callback"), "json").error(alert("error handler"));
This in the WEBrick:
Started POST "/articles" for 127.0.0.1 at 2014-09-15 09:19:49 +0100
Processing by ArticlesController#create as JSON
Parameters: {"article"=>{"title"=>"firsttitle", "text"=>"text of the article"}}
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.5ms | ActiveRecord: 0.0ms)
but again nothing happens in the browser. Any help?
remote: trueas an option in the form helper? This would turn the POST into an AJAX request to yourcreateorupdateaction in the controller. You could then create ancreate.js.erbfile in thearticlesview folder and it will run that JS.