我正在嘗試在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)
你在你的JS使用''#文件,但這些應該是'$' –
@Koen。謝謝,但它也行不通。 :c –
@DartNyan看起來像一切正常,你可以發佈你的服務器日誌請求 – 7urkm3n