私はRoRでAjaxを使用しようとしています。 私は何を間違えているのか教えてください。RailsでAjaxを試す
コントローラー:
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.js
else
format.js
end
end
end
create.js.erb:
$('#post_title').value('');
$('#post_content').value('');
$('#my_posts').prepend('<%= j render @post %>');
がindex.html.erb:
3210残念ながら、それは動作しません。 私を訂正してください、または良いと簡単なチュートリアルに助言してください。
UPD:
Started GET "/" for 127.0.0.1 at 2016-10-03 03:07:37 +0300
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PostsController#index as HTML
Rendering posts/index.html.erb within layouts/application
Post Load (0.1ms) SELECT "posts".* FROM "posts"
Rendered collection of posts/_post.html.erb [7 times] (1.5ms)
Rendered posts/index.html.erb within layouts/application (34.4ms)
Completed 200 OK in 269ms (Views: 257.9ms | ActiveRecord: 0.5ms)
Started POST "/posts" for 127.0.0.1 at 2016-10-03 03:07:44 +0300
Processing by PostsController#create as JS
Parameters: {"utf8"=>"✓", "post"=>{"title"=>"123", "content"=>"321"}, "commit"=>"Create Post"}
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "posts" ("title", "content", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "123"], ["content", "321"], ["created_at", 2016-10-03 00:07:44 UTC], ["updated_at", 2016-10-03 00:07:44 UTC]]
(143.9ms) commit transaction
Rendering posts/create.js.erb
Rendered posts/_post.html.erb (1.2ms)
Rendered posts/create.js.erb (2.5ms)
Completed 200 OK in 155ms (Views: 4.0ms | ActiveRecord: 144.1ms)
ファイルですが、それらは '$' –
@ Koenでなければなりません。ありがとうございますが、動作しません。 :c –
@DartNyanはうまくいくようですが、yrサーバーのログにリクエストを投稿することができます – 7urkm3n