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

質問編集履歴

2

補足をしました。

2020/01/23 09:01

投稿

yume0409
yume0409

スコア45

title CHANGED
File without changes
body CHANGED
@@ -53,4 +53,44 @@
53
53
  ちなみにエラーは出ていません。
54
54
  一体何が悪いのでしょうか?
55
55
 
56
- よろしくお願いします。
56
+ よろしくお願いします。
57
+
58
+
59
+ #補足
60
+ 回答ありがとうございます!
61
+ ```
62
+ def create
63
+ @tweet = Tweet.create(text: tweet_params[:text], user_id: current_user.id)
64
+ end
65
+
66
+ private
67
+ def tweet_params
68
+ params.permit(:text)
69
+ end
70
+ ```
71
+ createアクションはこのようにしました。
72
+ @tweet = を付け足して、redirect_to tweets_pathを消しました。
73
+ そしたら自動的にcreate.html.hamlが呼び出され、create.hrml.hamlは
74
+ ```
75
+ .chat-main
76
+ .tweets-block
77
+ .user-name
78
+ aaa
79
+ .messages
80
+ = @tweet.text
81
+ %i.fas.fa-ellipsis-h
82
+ = link_to "返信", "#", class: "reply"
83
+ .time
84
+ 時間
85
+ .tweets
86
+ = link_to new_tweets_path do
87
+ %i.fas.fa-pen
88
+ ```
89
+ このようにしました。
90
+ とりあえず、テキストの部分は反映されたのですが、その直後リロードするとshow.html.hamlに遷移され、
91
+ = @tweet.textを定義しても
92
+ ```
93
+ NoMethodError in Tweets#show
94
+ undefined method `text' for nil:NilClass
95
+ ```
96
+ というエラーが出ます。。。

1

誤字がありました。

2020/01/23 09:01

投稿

yume0409
yume0409

スコア45

title CHANGED
File without changes
body CHANGED
@@ -28,7 +28,7 @@
28
28
  なので反映させる記述がビューに必要?だと思いましたが、どういう風に記述していいのか、分かりません。。。
29
29
 
30
30
  #試した内容
31
- create.hrml.haml
31
+ create.html.haml
32
32
  ```
33
33
  .chat-main
34
34
  .tweets-block