LoginSignup
0
0

More than 3 years have passed since last update.

haml データベース取得 投稿機能表示

Last updated at Posted at 2020-07-02

アウトプット用です。
簡単な投稿機能の実装をしていたのですが若干詰まりました。

7104fd57826d56cd55a226533ee87e28.png

下記コードです。

top_page_controller.rb
class TopPageController < ApplicationController
  def index
    @toppages = TopPage.all
  end

  def new
    @toppage = TopPage.new
  end

  def create
    TopPage.create(TopPage.params)
    redirect_to top_page_index_path
  end

  def show
  end

  private
  def TopPage_params
    params.require(:TopPage).permit(:contents)
  end
end
top_page>index.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
      お悩み一覧
    .top__consultation__text
      = top_page.contents
      .top__consultation__text__time
        = top_page.created_at

データはパスしているし受け取るカラムも指定しているよな。。。?と思ってましたが
よくみたらtop_pageの定義していない事に気付きeach文を使ってみました

top_page>index.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
      お悩み一覧
  = @toppages.each do |top_page|    ←追記
    .top__consultation__text
      = top_page.contents
      .top__consultation__text__time
        = top_page.created_at

すると下記のようになりました。

7494b2ae155ade0fb54a8b676ea90dd5.png

成功したけどビュー崩れが泣

確かにTop_Page.allで全てデータをもってくるようにコントローラーは記述しましたが
実際の取得はcontentsしか指定してないのに何故全て表示されてしまうのか?

ただcontentsはしっかりとクラス内に表示されていて
何も定義されていないところに カラム名とデータが表示されています。
何となくhaml側の記述に問題があると思ってます。

ここが直せなかったのでまた明日です。

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