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

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

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

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

Q&A

解決済

2回答

2627閲覧

undefined method `permit' for "":Stringを解決したいです

sho-0451

総合スコア4

Ruby on Rails

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

0グッド

0クリップ

投稿2020/01/25 17:12

現在、ツイッターにアプリケーションを作成しており、コメント機能の実装の際、以下のエラーが起きました。

NoMethodError in CommentsController#create undefined method `permit' for "ssssssssssssssssssss":String private def comments_params params.require(:comment).permit(:text).merge(user_id: current_user.id, tweet_id: params[:tweet_id]) end end

該当のソースコード

comments_controller.rb

class CommentsController < ApplicationController def create @comment = Comment.new(comments_params) @comment.save redirect_to tweet_path(@comment.tweet.id) end private def comments_params params.require(:comment).permit(:text).merge(user_id: current_user.id, tweet_id: params[:tweet_id]) end end

20200125131637_create_comments.rb

class CreateComments < ActiveRecord::Migration[5.2] def change create_table :comments do |t| t.text :text, null: false t.integer :user_id, null: false, foreign_key: true t.integer :tweet_id, null: false, foreign_key: true t.timestamps end end end

show.html.haml

= render "homes/header" .tweetshow-box .tweetshow-header = image_tag @tweet.user.image.url, class: "tweetshow-userimage" .tweetshow-username = @tweet.user.name = image_tag @tweet.picture.url, class: "tweetshow-contentimage" .tweetshow-contenttext = @tweet.text .comment-formbox = form_with model: @comment, url: tweet_comments_path(@tweet.id), method: :post, local: true do |f| = f.text_area :text, class: "commentbox", placeholder: "コメントする" = f.submit "送信", class: "comment-btn" .comment-listcontainer - @comments.each do|comment| .comment-listbox .comment-listheader = image_tag comment.user.image.url, class: "comment-userimage" .comment-username = comment.user.name .comment-box = comment.comment

comments_paramsの書き方が間違っているのでしょうか?

補足

viewでcommentの表示を行う前はコメントを作成することができていました。
お時間に余裕がございましたら教えていただけるとありがたいです。

Rails 5.2.4.1

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

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

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

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

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

guest

回答2

0

show のアクションが掲載されていないので予測になってしまいますが、

= form_with model: @comment, url: tweet_comments_path(@tweet.id), method: :post, local: true do |f| = f.text_area :text, class: "commentbox", placeholder: "コメントする" = f.submit "送信", class: "comment-btn"

この部分の @commentnil になってはいませんか? model により送信する parameter の形式が変わるはずなので、

nil の場合

{ text: 'text text...' }

Comment のインスタンスが入っている場合

{ comment: { text: 'text text...' } }

のように変化するかと思います。

投稿2020/01/25 18:57

編集2020/01/25 18:58
unhappychoice

総合スコア1531

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

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

sho-0451

2020/01/26 02:23

textの部分がnillになっていました。 "text"=>"lam;m" nillの場合はどうすれば良いのでしょうか? 教えていただければ幸いです。 showの部分は以下のように定義しています。 @tweet = Tweet.find_by(id: params[:id]) @comments = Comment.all.order("created_at DESC")
guest

0

自己解決

showのアクションに
@comment = Comment.newを追加して解決することができました。

初歩的な、ミスでした。申し訳ありません。

回答して頂きありがとうございました。

投稿2020/01/26 04:07

sho-0451

総合スコア4

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問