質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

250閲覧

各記事に紐づいたユーザー情報を表示させたい。

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2018/08/14 07:01

前提・実現したいこと

Twitterのようなsnsを作ろうとしております。
現在scaffoldで記事を投稿する閲覧するようなページは作ってあります。
さらにdeviseでログイン認証については問題なく動きそうです。

ここからが問題なのですが各投稿に誰がいつ投稿したものなのかを表示したいのです。
しかし私初心者なもので検索してもどのようにすればよいのかがわかりません、
アソシエーションについては多少調べてみたものの合っているのかも分かりません。

どうかご教授いただけないでしょうか?

発生している問題・エラーメッセージ

該当のソースコード

ApplicationController

1class ApplicationController < ActionController::Base 2 before_action :authenticate_user! 3end

PostsController

1class PostsController < ApplicationController 2 before_action :set_post, only: [:show, :edit, :update, :destroy] 3 4 # GET /posts 5 # GET /posts.json 6 def index 7 @posts = Post.all 8 end 9 10 # GET /posts/1 11 # GET /posts/1.json 12 def show 13 end 14 15 # GET /posts/new 16 def new 17 @post = Post.new 18 end 19 20 # GET /posts/1/edit 21 def edit 22 end 23 24 # POST /posts 25 # POST /posts.json 26 def create 27 @post = Post.new(post_params) 28 29 respond_to do |format| 30 if @post.save 31 format.html { redirect_to @post, notice: 'Post was successfully created.' } 32 format.json { render :show, status: :created, location: @post } 33 else 34 format.html { render :new } 35 format.json { render json: @post.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /posts/1 41 # PATCH/PUT /posts/1.json 42 def update 43 respond_to do |format| 44 if @post.update(post_params) 45 format.html { redirect_to @post, notice: 'Post was successfully updated.' } 46 format.json { render :show, status: :ok, location: @post } 47 else 48 format.html { render :edit } 49 format.json { render json: @post.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /posts/1 55 # DELETE /posts/1.json 56 def destroy 57 @post.destroy 58 respond_to do |format| 59 format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 64 private 65 # Use callbacks to share common setup or constraints between actions. 66 def set_post 67 @post = Post.find(params[:id]) 68 end 69 70 # Never trust parameters from the scary internet, only allow the white list through. 71 def post_params 72 params.require(:post).permit(:tweet) 73 end 74end 75

index.html.erb

1<p id="notice"><%= notice %></p> 2 3<h1>Posts</h1> 4 5<table> 6 <thead> 7 <tr> 8 <th>Tweet</th> 9 <th colspan="3"></th> 10 </tr> 11 </thead> 12 13 <tbody> 14 <% @posts.each do |post| %> 15 <tr> 16 <td><%= post.tweet %></td> 17 <td><%= link_to 'Show', post %></td> 18 <td><%= link_to 'Edit', edit_post_path(post) %></td> 19 <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> 20 </tr> 21 <% end %> 22 </tbody> 23</table> 24 25<br> 26 27<%= link_to 'New Post', new_post_path %> 28

db\migrate\20180814045644_create_posts.rb

1class CreatePosts < ActiveRecord::Migration[5.2] 2 def change 3 create_table :posts do |t| 4 t.text :tweet 5 6 t.timestamps 7 end 8 end 9end

db\migrate\20180814050248_devise_create_users.rb

1# frozen_string_literal: true 2 3class DeviseCreateUsers < ActiveRecord::Migration[5.2] 4 def change 5 create_table :users do |t| 6 ## Database authenticatable 7 t.string :email, null: false, default: "" 8 t.string :encrypted_password, null: false, default: "" 9 10 ## Recoverable 11 t.string :reset_password_token 12 t.datetime :reset_password_sent_at 13 14 ## Rememberable 15 t.datetime :remember_created_at 16 17 ## Trackable 18 t.integer :sign_in_count, default: 0, null: false 19 t.datetime :current_sign_in_at 20 t.datetime :last_sign_in_at 21 t.string :current_sign_in_ip 22 t.string :last_sign_in_ip 23 24 ## Confirmable 25 # t.string :confirmation_token 26 # t.datetime :confirmed_at 27 # t.datetime :confirmation_sent_at 28 # t.string :unconfirmed_email # Only if using reconfirmable 29 30 ## Lockable 31 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 32 # t.string :unlock_token # Only if unlock strategy is :email or :both 33 # t.datetime :locked_at 34 35 36 t.timestamps null: false 37 end 38 39 add_index :users, :email, unique: true 40 add_index :users, :reset_password_token, unique: true 41 # add_index :users, :confirmation_token, unique: true 42 # add_index :users, :unlock_token, unique: true 43 end 44end 45

試したこと

一度アソシエーションを使って紐づけなのを行ってみたのですが、うまくいかずもう一度一から作り直しています。

補足情報(FW/ツールのバージョンなど)

右も左もわかりません。追記したほうが良いコードなどございましたら教えていただけると幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Railstutorialで似たようなチャプターがあります。参考にしてください。
Railstutorial 13章

投稿2018/08/14 07:30

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問