尝试在Rails中使用Ajax

问题描述:

我正在尝试在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

所以,你的代码看起来不错,除了它需要删除
$('#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'来查看是否有任何区别。让我们从那里开始调试,这是我现在可以看到的第一个区别。如果这不起作用,请告诉我 –