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

回答編集履歴

2

疑問形式に

2020/12/10 07:54

投稿

FKM
FKM

スコア3694

answer CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  def create
9
9
  @community = current_user.communities.build(community_params)#ストロングパラメータを呼び出す
10
- if @community.save #はオブジェクト?
10
+ if @community.save //メソッド?オブジェクト?
11
11
  redirect_to communities_path(@community), notice: "投稿に成功しました。"
12
12
  else
13
13
  render :new
@@ -15,7 +15,7 @@
15
15
  end
16
16
 
17
17
  def edit
18
- if @community.user_id != current_user.id #ここはオブジェクトだとわかる
18
+ if @community.user_id != current_user.id //ここはオブジェクトだとわかる
19
19
  redirect_to communities_path, alert: '不正なアクセスです。'
20
20
  end
21
21
  end

1

メソッドとオブジェクト

2020/12/10 07:54

投稿

FKM
FKM

スコア3694

answer CHANGED
@@ -1,14 +1,22 @@
1
1
  エラーを示しているのはif文の判定部分ですね。だとするとcommunity.saveというメソッドが本当に存在するか確認した方がいいです。ひょっとしたら他の変数と紛れてオブジェクトとメソッドがごっちゃになっている可能性は考えられます。
2
2
 
3
3
  ```rb:rb
4
+ def new
5
+ @community = Community.new //ここはメソッド
6
+ end
4
7
 
5
8
  def create
6
9
  @community = current_user.communities.build(community_params)#ストロングパラメータを呼び出す
7
- if @community.save #ここがエラー
10
+ if @community.save #これはオブジェクト?
8
11
  redirect_to communities_path(@community), notice: "投稿に成功しました。"
9
12
  else
10
13
  render :new
11
14
  end
12
15
  end
13
16
 
17
+ def edit
18
+ if @community.user_id != current_user.id #ここはオブジェクトだとわかる
19
+ redirect_to communities_path, alert: '不正なアクセスです。'
20
+ end
21
+ end
14
22
  ```