0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

マイページ(自分の投稿だけ見える)実装

Posted at

備忘録アウトプット

投稿機能が先日実装できたので、次は自分の投稿だけ見えるようにするマイページ実装を行いました。
試行錯誤し下記コードに到達

header.html.haml
.header
  = link_to "Answer", root_path ,class: "header__title" 
  -if user_signed_in?
    = link_to "投稿", '/top_page/new' , class: "header__post"
    = link_to "マイページ", '/users/#{current_user.id}', class: "header__mypage"
    .header__search
      検索 
  -else
    .header__search
    .header__search
    .header__search
    .header__search
    .header__search
    .header__search
      検索 
qiita.rb
class UsersController < ApplicationController
  def index
  end

  def edit
  end

  def update
  end

  def show
    @responses = current_user.top_pages
  end
end
users>show.html.haml
.top
  %ul.top__side
    ジャンル
    %li.top__side__love
      恋愛
    %li.top__side__work
      仕事
    %li.top__side__money
      お金
    %li.top__side__friend
      友人
    %li.top__side__school
      学校
    %li.top__side__other
      その他
  .top__consultation
    .top__consultation__title
      お悩み一覧
    - @responses.each do |responses|
      .top__consultation__text
        = link_to responses.contents, "/user/#{responses.id}", method: :get
        .top__consultation__text__main
          .top__consultation__text__main__name
            = responses.user.name
          .top__consultation__text__main__time
            = responses.created_at

するとこんな感じに
a0664f72d2380a27a88030132e0698e6.png

エラーは覚悟していたが、まさかのルーティング。え?と思いながらrouteを確認しても間違いらしきものは見つからず。
ふとマイページのURLが目に入ったのですがこんな感じに。
http://localhost:3000/users/#{current_user.id}

ログインユーザーを識別するコードを書いたのですが、何故文字列のままになっているんだ??
と疑問を感じつつリンクに繋がるコードを確認しました。(ファイルで言うところのheader.html.hamlですね)
で、原因はこれ

    = link_to "マイページ", '/users/#{current_user.id}', class: "header__mypage"

正直書いたときから、違和感はありました(言い訳)
これはダブルクォーテーションですね!!

というか使い方もよくわかっていなかったので改めて調べました。

シングルクォーテーションは文字列にする場合
ダブルクォーテーションは変数や計算式などが付与される場合
で使い分けるそうです、ざっくばらんですけど。

であればこれシングをわざわざ使う事ってあるんですかね?
よくわかりません。全部ダブルでよくない? 可読性とか?
知ってるかたいたら教えてください。

まぁとりあえずここをダブルに直したら表示はされましたとさ。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?