2017-01-17 3 views
0

正確にどのように呼び出されたのかわかりませんが、一例です。Rails Js、ページ内のレンダリングページ

投稿を作成する可能性があり、「保存」と「プレビュー」の2つのボタンがあります。

「プレビュー」ユーザーがページを離れることはしませんが、JSを使用すると、ポストビューの新しいHTMLのボタンの直後にレンダリングされます。

あなたはそれがどのように行われたのか一般的な考えを教えてもらえますか?

+0

なぜカントを使用するとajaxコールを使用するモーダルポップアップで表示する...ポストIDとコールショーリクエストを使用するだけです。 – Milind

+0

ターボリンクを使ってできませんか? –

答えて

0

はあなたのビュー

...あなただけのボタンの近くにプレビュー]ボタンをクリックして上の記事のプレビューを取得したい場合、これはどのようにすべきである...

とさせて頂きます
<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> 

view.jsスクリプトにあります。ポスト/ show.js.erb

##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_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) { 

      }); 

}) 

posts_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 

これは、あなたが何を探しているされていること。)/

+0

私はそれが、私は一般的な考えを得ると思う、私はそれを使用することができます参照してください。しかし、 'var post_id = $(this).data(" post_id ");' - 私たちは投稿IDを持っていません。まだDBに保存していませんでした。 –

+0

ifsが保存されていない場合は、関連する情報をそのまま渡してください。ところで...プレビューボタンをクリックする前に、あなたはどの情報を持っていますか? ? – Milind

+0

私はDBに何をスタックしているのかを言うならば、none(前に保存する必要があります)。しかし、あなたが '@ post.title'や' @ post.desscription'などのフィールドを埋めると言ったら、それはそれです。 –

関連する問題