teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コード追加

2019/11/26 09:33

投稿

MISIAN-MISIAN
MISIAN-MISIAN

スコア73

title CHANGED
File without changes
body CHANGED
@@ -41,4 +41,84 @@
41
41
  = submit_tag 'Search', name: nil
42
42
  - @tweets.each do |tweet|
43
43
  .main-photo{style: "background-image: url(#{tweet.image});"}
44
+ ```
45
+
46
+ ```ここに言語を入力
47
+ ファイル<routes.rb>
48
+ Rails.application.routes.draw do
49
+
50
+ devise_for :users
51
+ root to: "tweets#index"
52
+ namespace :tweets do
53
+ resources :searches, only: :index
54
+ end
55
+ resources :tweets do
56
+ resources :comments, only: [:create, :destroy]
57
+ end
58
+ resources :users, only: :show
59
+
60
+ end
61
+ ```
62
+
63
+ ```ここに言語を入力
64
+ ファイル名<tweets.controller.rb>
65
+ class TweetsController < ApplicationController
66
+ before_action :set_tweet, only: [:edit, :show]
67
+ before_action :move_to_index, except: [:index, :show]
68
+
69
+ def index
70
+ @tweets = Tweet.includes(:user).order("created_at DESC").page(params[:page]).per(6)
71
+ @random = Tweet.order("RAND()").limit(1)
72
+
73
+ end
74
+
75
+ def new
76
+ @tweet = Tweet.new
77
+ end
78
+
79
+ def create
80
+ Tweet.create(tweet_params)
81
+
82
+ end
83
+
84
+ def destroy
85
+ tweet = Tweet.find(params[:id])
86
+ tweet.destroy
87
+
88
+ end
89
+
90
+ def edit
91
+
92
+ end
93
+
94
+ def update
95
+ tweet = Tweet.find(params[:id])
96
+ tweet.update(tweet_params)
97
+ end
98
+
99
+ def show
100
+ @comment = Comment.new
101
+ @comments = @tweet.comments.includes(:user)
102
+ end
103
+
104
+ def search
105
+ @tweets = Tweet.search(params[:keyword])
106
+ end
107
+
108
+ private
109
+ def tweet_params
110
+ params.require(:tweet).permit(:image, :text).merge(user_id: current_user.id)
111
+ end
112
+
113
+ def set_tweet
114
+ @tweet = Tweet.find(params[:id])
115
+ end
116
+
117
+ def move_to_index
118
+ redirect_to action: :index unless user_signed_in?
119
+ end
120
+
121
+
122
+ end
123
+
44
124
  ```