LoginSignup
1
4

More than 5 years have passed since last update.

railsで投稿後にSNSシェアページへ遷移させる

Posted at

やりたいこと

投稿後にSNSでシェアしてねと投稿が完了しましたという表示をするページに遷移させたい

具体的な仕様

  • 投稿完了画面でシェアボタンを実装したい
  • シェアするページは各投稿詳細ページ
  • 投稿詳細ページはposts/idのURLで各idがurlに入っている

実装方法

  • urlにidを入れ、paramsで受け渡しをする
  • urlにid入れてうつと誰でも遷移できるが別に投稿完了画面に個人情報は無いのでそれはよしとする

投稿完了画面のview作成

view(html.erb)を作成。既にcontrollerがあるならコマンド打つ必要無し。

routesに下記を記入

routes.rb
get "posts/:id/done" => "posts#done"

別に投稿完了ページにページ固有のコンテンツは無いがidを渡すためにurlにidを入れる。

controllerにdoneを作成

posts_controller.rb
  def done
    @post_id = params[:id]
  end

viewに変数を

done.html.erb
<div class="row">
  <a href="http://twitter.com/share?url=url/posts/<%= @post_id %>">
  <button type="button" class="btn btn-primary btn-lg btn-block col-xs-12">Twitterでシェア</button>
</div><!--row-->
<div class="row done_tohome">
  <a href="/posts/<%= @post_id %>">
  <button type="button" class="btn btn-secondary btn-lg btn-block col-xs-12">投稿詳細ページに戻る</button>
  </a>
</div><!--row-->
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4