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
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>
<%= link_to image_tag(post.image_url.to_s), post_path(post), remote: true %>
–
Glazer respond_to do |format| format.js end
solved the issue. Now the show link works great, and modal still works 👍 I just need to apply URL to address bar somehow when we view post in modal. –
Glazer © 2022 - 2024 — McMap. All rights reserved.