2016-10-02 51 views
1

我正在嘗試在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) 
+1

你在你的JS使用''#文件,但這些應該是'$' –

+0

@Koen。謝謝,但它也行不通。 :c –

+0

@DartNyan看起來像一切正常,你可以發佈你的服務器日誌請求 – 7urkm3n

回答

1

所以,你的代碼看起來不錯,除了它需要刪除
$('#post_title').value('');
$('#post_content').value('');

我重建項目和它的工作時,我刪除的那些行,這些行碼。不知道爲什麼你首先需要它們,因爲當你點擊create動作時,javascript首先呈現@post,它將使用post partial爲新創建的post object/(@post)創建HTML代碼。最後,HTML代碼預先添加到顯示posttitlecontent#my_posts元素中,所以我不確定這些行應該完成什麼。希望這應該解決的問題

以防萬一,這是什麼樣子

我的代碼

test_app /應用/控制器/ posts_controller.rb

class PostsController < ApplicationController 
    def index 
    @post = Post.new 
    @posts = Post.all 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(post_params) 
    respond_to do |format| 
     if @post.save 
     format.js 
     else 
     format.js 
     end 
    end 
    end 

    def post_params 
    params.require(:post).permit(:title, :content) 
    end 
end 

test_app /應用/視圖/文章/ index.html.erb

<%= form_for @post, remote: :true do |f| %> 
    <div class="my_form"> 
    <%= f.label :title %> 
    <%= f.text_field :title, class: 'text' %> 
    <%= f.label :contnet %> 
    <%= f.text_field :content, class: 'text' %> 
    <%= f.submit 'Submit' %> 
    </div> 
<% end %> 

<h1>Posts</h1> 

<table class="table"> 
    <thead> 
    <tr> 
     <th>Title</th> 
     <th>Content</th> 
     <th colspan="3"></th> 
    </tr> 
    </thead> 

    <tbody id='my_posts'> 
    <%= render @posts %> 
    </tbody> 
</table> 

test_app /應用/視圖/職位/ _post.html.erb

<tr> 
    <td> 
    <%= post.title %> 
    </td> 
    <td> 
    <%= post.content %> 
    </td> 
</tr> 

test_app /應用/視圖/職位/ create.js.erb

$("#my_posts").prepend('<%= j render @post %>') 
$('.text').val('') 
+0

男人,我很感激。這是因爲$('#post_title')。value(''); $('#post_content')。value('');' –

+0

但是我想在創建帖子後清除我的輸入。我該怎麼做? –

+0

提交帖子後,他們應該清楚。當我重新創建這個應用程序時,我的文本字段在提交後很清晰。您可以與我的代碼進行交叉檢查,我不知道您是否有任何其他JavaScript文件可能會導致此問題,但理想情況下文本輸入在提交後應該清楚。我從來沒有使用'simple_form_for',但是你可以嘗試使用'form_for'來查看是否有任何區別。讓我們從那裏開始調試,這是我現在可以看到的第一個區別。如果這不起作用,請告訴我 –