2017-11-18 145 views
0

我的主頁上顯示的職位名單,當我點擊一個帖子,它作爲一個新的頁面打開,如何使用Ruby一個模式打開後顯示頁面on Rails的5

,但我需要留在主頁並打開帖子顯示頁面爲模態。任何想法如何?

謝謝

+0

可能是最有效的方式來做到這一點是使用AJAX。您可以向服務器發送一個AJAX請求,然後獲取結果並使用它來填充模式。如果你只在頁面上有幾個帖子,你可以簡單地在一個頁面上渲染所有的模塊,然後使用javascript來隱藏和顯示它們。 –

+0

嗯,謝謝你的指導,我會研究一下AJAX。我有數百件物品。 – Designer

回答

1

解決方案與您所使用的自舉的假設......

改變你的節目了JS行動要求

def show 
    @post = Post.find(params[id]) 
    respond_to do |format| 
     format.js 
    end 
    end 

添加這個div在帖子指數結束

<div id='post-content'></div> 

您的鏈接顯示頁面必須看起來像這樣現在

<%= link_to 'View Post', post_path(post), remote: true %> 

添加新的文件帖/ show.js.erb

$('#post-content').html("<%= j render 'post_modal', post: @post %>"); 
$('#post-modal').modal('show'); 

增加部分職位/ _post_modal.html.erb

<div id="post-modal" class="modal fade" role="dialog"> 
    <div class="modal-dialog modal-lg"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     </div> 

     <div> 
     --- put all your show.html.erb code here 
     </div> 
    </div> 
    </div> 
</div> 
+0

謝謝隊友!我沒有使用Bootstrap,我仍然會嘗試從你的方式解決。謝謝 – Designer

+0

哇!它實際上工作!謝謝!我正在研究你現在學習的方法。謝謝!順便說一句,你有什麼建議我如何保持URL,以便它可以共享?目前我收到錯誤https://i.imgur.com/9wjw720.png – Designer

+0

它看起來像你錯過了遙控器:true在你的link_to標記<%= link_to'View Post',post_path(post),remote:true%> –

相關問題