Javascript Rails Js,在页面中呈现页面
我不知道它的确切名称,但作为一个例子 我有可能创建文章,我有两个按钮-“保存”和“预览” 我想当用户点击“预览”时,用户不会离开页面,但使用JS,它会在按钮后立即呈现,使用post视图创建新的htmlJavascript Rails Js,在页面中呈现页面,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,我不知道它的确切名称,但作为一个例子 我有可能创建文章,我有两个按钮-“保存”和“预览” 我想当用户点击“预览”时,用户不会离开页面,但使用JS,它会在按钮后立即呈现,使用post视图创建新的html 你能给我一个大概的想法吗?让我们假设……如果你想通过点击按钮附近的预览按钮来预览文章,那么这就是应该的方式…… $("#show_preview").on("click",function(){ //call ajax to show preview of the post in the pre
你能给我一个大概的想法吗?让我们假设……如果你想通过点击按钮附近的预览按钮来预览文章,那么这就是应该的方式……
$("#show_preview").on("click",function(){
//call ajax to show preview of the post in the preview container
var post_id = $(this).data("post_id");
//this is the show call to posts_controller#show
$.ajax({
url: "/posts/'"+post_id,
beforeSend: function( xhr ) {
//change text of a tag to loading
}
})
.done(function( data ) {
});
})
你的观点
<a href="<%= preview_path(@post)%>" id="show_preview" data-post_id="<%= @post.id%>"> title="click to preview this post">
Preview
</a>
<button type="submit">Submit</button>
<!-- this is the container that will show the preview post -->
<div id="preview_container">
</div>
在posts\u controller.rb中
def show
##you might already be using id param to get post
@post = Post.find(param[:id])
##dont wory about rendering show.js.erb,rails will understand what type of request is it and then render show.html.erb or show.js.erb.so just create show.js.erb to replace and fill the preview container
end
##just render _show.html.erb inside the container
$("#preview_container").html("<%= escape_javascript(render(:partial => 'post/show.html.erb' %>");
write you html in _show.html.erb that will be shown in the preview_container
在posts/show.js.erb中
def show
##you might already be using id param to get post
@post = Post.find(param[:id])
##dont wory about rendering show.js.erb,rails will understand what type of request is it and then render show.html.erb or show.js.erb.so just create show.js.erb to replace and fill the preview container
end
##just render _show.html.erb inside the container
$("#preview_container").html("<%= escape_javascript(render(:partial => 'post/show.html.erb' %>");
write you html in _show.html.erb that will be shown in the preview_container
##只需在容器内呈现_show.html.erb即可
$(“#preview_container”).html(“'post/show.html.erb'>”;
在预览容器中显示的_show.html.erb中编写html
希望这就是您所看到的:)/为什么不能使用ajax调用并在模式弹出窗口中显示它…只需使用post id和call show请求。您不能使用Turbolinks吗?如果您说的是我在DB中堆栈的内容,那么就没有了(之前需要保存)。但是如果你说的是填写字段,那么,例如,
@post.title
,@post.desscription
,就是这样。如果是这样,就很容易了。让我们快速聊天。我邀请你聊天……而不是每次都发表评论:)我们去聊天吧,但我不知道如何创建和邀请聊天室,你能做到吗?