Ad

How To Share A Post From Your Blog On Social Media By Post Id In Rails

- 1 answer

I have this index.html.erb, which makes my posts menu:

<%provide(:title, "Recipes") %>



<div class="search_container">
   <%= form_tag(posts_path, :method => 'get', id: "search-form") do %>
     <%= text_field_tag :search,params[:search], placeholder: "Search Tags" %>
     <%= submit_tag "Search" %>
   <% end %>
</div>
   <div class="tag_container">

     <div class="tag_cloud tag_cloud_positioning_and_color_and_stuff">
       <% tag_cloud Post.tag_counts, %w{s m l} do |tag, css_class| %>
         <%= link_to tag.name, tag_path(tag.id), class: css_class %>
       <% end %>
     </div>
   </div>

<div class="posts_wrapper">
  <% @posts.each do |post| %>
  <div class="card-group card-group-size">
    <div class="card recipes recipes_posts">
      <%= link_to post do %>
       <img src="<%= url_for(post.image) if post.image.attached?  %> " class="index_images">
      <% end %>
      <div class="card-body card_body_size">
        <h5 class="card-title"><%=link_to post.title, post, class: "post_title"%></h5>
      </div>
    </div>
  </div>
  <% end %>
  <%= will_paginate @posts %>
</div>

When i chose a post from the menu the show.html.erb is rendered following the specific post_id. In this format it shares the right Post whit the image attached: (it is modified after benj-p gave me the ideea with <%= posts_path(@post) %>):

<head>

  <meta property="og:url"           content="https://secretstrawberry.herokuapp.com/posts/<%= @post.id %>" />
  <meta property="og:type"          content="website" />
  <meta property="og:title"         content="<%= @post.title %>" />
  <meta property="og:image"         content="<%= image_tag @post.image %>" />
</head>




<div id="fb-root"></div>
<script async defer crossorigin="anonymous"
 src="https://connect.facebook.net/en_GB</sdk.js#xfbml=1&version=v7.0&appId=2751510331745380&autoLogAppEvents=1">
</script>



<%provide(:title, @post.title) %>

<div class="recipe_container">


  <% if logged_in? && current_user.admin? %>
    <div class="edit_delete_buttons_container">
      <div class="edit_delete_buttons">
        <%= link_to "Edit", edit_post_path(@post), {:class => "img_button btn btn-success button_back"} %>
      </div>
      <div class="edit_delete_buttons">
        <%= link_to "Delete", post_path(@post), method: :delete, data: { confirm: "Are you sure?" },
                    class: "img_button btn btn-danger" %>
      </div>
    </div>
  <% end %>


<div class="fb-share-button" data-target="_blank" rel="nofollow noreferrer" href="https://secretstrawberry.herokuapp.com/posts/<%= @post.id %>"
data-layout="button_count" data-size="small"><a target="_blank"
target="_blank" rel="nofollow noreferrer" href="https://www.facebook.com/sharer/sharer.php?u=<%= posts_path(@post) %>&amp;src=sdkpreparse"
class="fb-xfbml-parse-ignore">Share</a></div>


  <div class="tags tags_in_recipe_show">
    Tags:<%= render @post.tags %>
  </div>
  <div class="signup_h1  post_new_button h1_show">

    <h1 id=""><strong><%= @post.title %></strong></h1>



  </div>



  <div class="image_container">

    <% if @post.image.attached? %>
      <%= image_tag @post.image, class: "image_view" %>
    <% end %>
  </div>



  <div class="recipe_body">


    <div class="margin_top_ingredients">
      <pre class="ingredients">
              <%= @post.content %>
       </pre>
    </div>

    <div>
      <pre class="ingredients">
              <%= @post.method %>
      </pre>
    </div>

    <div>
      <pre class="ingredients">
               <%= @post.description %>
      </pre>
    </div>




  </div>





    <div class="recipe_body comments_container ">

      <p id="comment_count"><%= @post.comments.count %> reviews  <%= link_to "Log In", login_path unless logged_in? %> </p>

      <%= render @post.comments.order("created_at DESC").paginate(page: params[:page], per_page: 5)%>


      <div class="form_container_post">
          <%= render 'comments/form' %>

        <div class="paginate">
          <%= will_paginate @comments %>
        </div>
      </div>
    </div>

</div>

Now this facebook share button is working, sharing the Post title and the post image.

Thank you.

Ad

Answer

You can dynamically get the url of the post and interpolate it in the Facebook share url.

 <a target="_blank" target="_blank" rel="nofollow noreferrer" href="https://www.facebook.com/sharer/sharer.php?u=<%= post_url(@post) %>&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a>
Ad
source: stackoverflow.com
Ad