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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

679閲覧

グループチャットでグループの編集機能を実装する

kz_oita0714

総合スコア5

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/01/24 00:10

###実現したいこと
グループチャットでグループの編集機能を実装しようとしているが、
以下のエラーにより、実装ができない

###エラー内容
NoMethodError in GroupsController#update
undefined method `update' for nil:NilClass
イメージ説明

###ソースコード
route.rb

Rails.application.routes.draw do devise_for :users root to: "groups#index" resources :users, only: [:edit, :update] resources :groups, only: [:index,:new, :create, :edit, :update] do resources :messages,only: [:index, :create] end end

group.controller.rb

def index end def new @group = Group.new @group.users << current_user end def create @group = Group.new(group_params) if @group.save redirect_to root_path, notice: 'グループを作成しました' else render :new end end def edit @group = Group.find(params[:id]) end def update @gruops = Group.find(params[:id]) if @group.update(group_params) redirect_to group_messages_path(group), notice: 'グループを更新しました' else render :edit end end private def group_params params.require(:group).permit(:name, user_ids: []) end end

form.html.hanl(グループを編集)

= form_for group do |f| - if group.errors.any? .chat-group-form__errors %h2= "#{group.errors.full_messages.count}件のエラーが発生しました。" %ul - group.errors.full_messages.each do |message| %li= message .chat-group-form__field .chat-group-form__field--left = f.label :name, class: 'chat-group-form__label' .chat-group-form__field--right = f.text_field :name, class: 'chat__group_name chat-group-form__input', placeholder: 'グループ名を入力してください' .chat-group-form__field.clearfix .chat-group-form__field.clearfix .chat-group-form__field--left %label.chat-group-form__label{:for => "chat_group_チャットメンバー"} チャットメンバー .chat-group-form__field--right = f.collection_check_boxes :user_ids, User.all, :id, :name .chat-group-form__field.clearfix .chat-group-form__field--left .chat-group-form__field--right = f.submit class: 'chat-group-form__action-btn'

よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

updateの下のところは、@gruops = Group.find(params[:id])ではなくて、@group = Group.find(params[:id])ではないでしょうか。
なので、@groupがnilとなり、エラーになっているんだと思います。

投稿2020/01/24 01:23

編集2020/01/24 01:24
clora

総合スコア72

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

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

kz_oita0714

2020/01/24 13:36

回答のとおり修正したら、動きました!ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問