Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在不重新加载页面的情况下提交类似twitter的micropost并显示帖子?_Jquery_Ruby On Rails_Ruby_Ajax - Fatal编程技术网

Jquery 如何在不重新加载页面的情况下提交类似twitter的micropost并显示帖子?

Jquery 如何在不重新加载页面的情况下提交类似twitter的micropost并显示帖子?,jquery,ruby-on-rails,ruby,ajax,Jquery,Ruby On Rails,Ruby,Ajax,我正在尝试提交一个micropost,并在页面顶部显示它,而不需要重新加载页面,但是我在让它工作时遇到了很多麻烦。任何解释和解决方案都将不胜感激 我有一个用户有很多微操作 解决方案 https://github.com/sunwooz/fasttwitter/pull/1 Here is a working implementation let me go thru the changes really quick fasttwitter/app/views/layouts/applicat

我正在尝试提交一个micropost,并在页面顶部显示它,而不需要重新加载页面,但是我在让它工作时遇到了很多麻烦。任何解释和解决方案都将不胜感激

我有一个用户有很多微操作

解决方案

https://github.com/sunwooz/fasttwitter/pull/1

Here is a working implementation let me go thru the changes really quick

fasttwitter/app/views/layouts/application.html.erb
Since rails 3.1 you should use "application" instead of "defaults" apparently this was the main       problem, because it generated an error stopped the remote => true from working
fasttwitter/app/views/microposts/create.js.coffee
There is no need for this file. rails is going to check for a .erb file not a .coffee
fasttwitter/app/views/microposts/create.js.erb
I had some problems with the partial rendering, but it is working with the html you posted on     Stackoverflow
这是show.html.erb(用户)


我建议您添加以下内容

上的javascript响应创建控制器操作的
操作,以防没有。它可以简单到

respond_to { |format| format.js }
还要确保在表单上保留
:remote=>true

通过向
js
添加response\u,控制器将尝试查找位于
app/views/model\u mulral/create.js.erb
下的文件

js.erb
文件与
html.erb
一样,但是您可以将Ruby嵌入js,而不是将Ruby嵌入html

现在,您可以创建类似这样的js视图

app/views/microscops/create.js.erb

<% if @micropost.errors.any? %>
  alert('errors');
<% else %>
    $('#microposts').prepend('<div class="micropost"><h3>' +  "<%= j @micropost.user.email%>" + '</h3><h4>Posted On: ' + "<%= j @micropost.created_at %>" + '</h4><p>' +  "<%= j @micropost.content %>" + '</p></div>');
<% end %>

警报(“错误”);
$(“#microposts”).prepend(“++”发布在:“++”“++”

”);

希望有帮助:)

我建议您添加以下内容

上的javascript响应创建控制器操作的
操作,以防没有。它可以简单到

respond_to { |format| format.js }
还要确保在表单上保留
:remote=>true

通过向
js
添加response\u,控制器将尝试查找位于
app/views/model\u mulral/create.js.erb
下的文件

js.erb
文件与
html.erb
一样,但是您可以将Ruby嵌入js,而不是将Ruby嵌入html

现在,您可以创建类似这样的js视图

app/views/microscops/create.js.erb

<% if @micropost.errors.any? %>
  alert('errors');
<% else %>
    $('#microposts').prepend('<div class="micropost"><h3>' +  "<%= j @micropost.user.email%>" + '</h3><h4>Posted On: ' + "<%= j @micropost.created_at %>" + '</h4><p>' +  "<%= j @micropost.content %>" + '</p></div>');
<% end %>

警报(“错误”);
$(“#microposts”).prepend(“++”发布在:“++”“++”

”);

希望对您有所帮助:)

首先,非常感谢您的帮助!我创建了javascript文件create.js.erb,并在micropost控制器的创建操作中添加了respond_to format.js行。我将表单设置为:Remote=>true,但现在它正在重新加载页面并提交micropost。我是否必须删除micropost部分和旧的micropost.js文件?不知道是否需要删除js文件,但肯定不再需要它。您在app/views/microscops/create.js.erb?…上创建了
js.erb
文件?。。。。。嘿,实际上是另一个问题,哪个控制器实际上在处理你的应用程序上的post请求(Userr或Microposts)?是的,在Microposts视图中创建.js.erb。我发布了micropost控制器/创建操作。我向您发送了一个拉取请求,请检查它以开始,非常感谢您的帮助!我创建了javascript文件create.js.erb,并在micropost控制器的创建操作中添加了respond_to format.js行。我将表单设置为:Remote=>true,但现在它正在重新加载页面并提交micropost。我是否必须删除micropost部分和旧的micropost.js文件?不知道是否需要删除js文件,但肯定不再需要它。您在app/views/microscops/create.js.erb?…上创建了
js.erb
文件?。。。。。嘿,实际上是另一个问题,哪个控制器实际上在处理你的应用程序上的post请求(Userr或Microposts)?是的,在Microposts视图中创建.js.erb。我发布了micropost控制器/创建操作。我已经向您发送了一个拉取请求,请查看
respond_to { |format| format.js }
<% if @micropost.errors.any? %>
  alert('errors');
<% else %>
    $('#microposts').prepend('<div class="micropost"><h3>' +  "<%= j @micropost.user.email%>" + '</h3><h4>Posted On: ' + "<%= j @micropost.created_at %>" + '</h4><p>' +  "<%= j @micropost.content %>" + '</p></div>');
<% end %>