My homepage displays a list of posts and when I click a post, it open as a new page,
but I need to stay at the homepage and open post Show page as modal. Any idea how?
Thank you
Solution is with the assumption that you are using bootstrap...
change your show action for js requests
def show
@post = Post.find(params[id])
respond_to do |format|
format.js
end
end
add this div at the end of posts index
<div id='post-content'></div>
your link to show page must look something like this now
<%= link_to 'View Post', post_path(post), remote: true %>
add a new file posts/show.js.erb
$('#post-content').html("<%= j render 'post_modal', post: @post %>");
$('#post-modal').modal('show');
add a partial posts/_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">×</button>
</div>
<div>
--- put all your show.html.erb code here
</div>
</div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With