Home>

I am making a web application that posts a short sentence like twitter.
The development language and framework is Ruby on Rails.

The contents of the text posted to the application you created can be shared on twitter by those who visited the site.
Therefore, I want to be able to change the content that users share for each content of the text posted on my site.

source code:
<% @ posts.each do | post |%>
 <% = post.body%>
 
 <a href = "https://twitter.com/share" data-url = "your site URL" data-text = "I want to change this part for each post.body." data- via = "username" data-lang = "en" data-size = "large" data-related = "username" data-hashtags = "lyric" data-dnt = "true">tweet</a><script>! function (d, s, id) {var js, fjs = d.getElementsByTagName (s) [0], p =/^ http: /. test (d.location)? 'http': 'https';if (! d.getElementById (id)) {js = d.createElement (s);js.id = id;js.src = p + ': //platform.twitter.com/widgets.js';fjs.parentNode. insertBefore (js, fjs);}} (document, 'script', 'twitter-wjs');</script>
<% end%>


The code is written in index.html.erb.
It receives @posts from the controller and displays it with each.
Contains the text of what has been posted to post.body.

I want me to answer

A share button is attached to each post.
The content shared by twitter enters data_text, but I want to change this for each value of post.body.
How can I put the value that can be obtained in post.body into the body of the shared twitter?

Supplement

URL of the twitter that fetched the HTML snippet

Rails 5.0.0.1
ruby 2.3.1

  • Answer # 1

    In the case of erb, you can embed it in html without any problem!

    <a href = "https://twitter.com/share" data-url = "your site URL" data-text = "<% = post .body%>"data-via =" username "data-lang =" en "data-size =" large "data-related =" username "data-hashtags =" lyric "data-dnt =" true ">Tweet</a><script>! Function (d, s, id) {var js, fjs = d.getElementsByTagName (s) [0], p =/^ http: /. Test (d.location)? 'http': 'https';if (! d.getElementById (id)) {js = d.createElement (s);js.id = id;js.src = p +': //platform.twitter.com/widgets .js';fjs.parentNode.insertBefore (js, fjs);}} (document, 'script', 'twitter-wjs');</script>