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

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

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

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Ruby on Rails

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

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

Q&A

1回答

1041閲覧

undefined local variable or method `post_id' エラーを改善しフォロー機能実装を達成したい

shunlab

総合スコア2

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Ruby on Rails

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

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

0グッド

0クリップ

投稿2020/05/07 16:38

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
rails初学者です。
初歩的な質問すみません
丸一日足止めを食らっています。。
現在、railsにてフォロー機能の追加を行なっております。

viewにユーザー画面一覧を追加したところ以下のエラーが発生しました。

発生している問題・エラーメッセージ

イメージ説明

### 該当のソースコード コード
<div class="row"> <div class="col-md-4 text-center"> <%= image_tag avatar_url(@user), class: "round-img" %> <% if user_signed_in? %> <h1>ユーザー一覧画面</h1> <% @users.each do |user| %> <p> <%= "id: #{user.id} email: #{user.email}" %> <% if current_user.following?(user) %> <%= link_to 'フォロー外す', unfollow_path(user.id), method: :POST %> <% else %> <%= link_to 'フォローする', follow_path(user.id), method: :POST %> <% end %> </p> <% end %> <% end %> </div> <div class="col-md-8"> <div class="row"> <h1><%= @user.name %></h1> <%= link_to "プロフィールを編集", edit_user_registration_path, class: "btn btn-outline-dark common-btn edit-profile-btn" %> <button type="button" class="setting" data-toggle="modal" data-target="#exampleModal"></button> <h1>ユーザー一覧画面</h1> <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">設定</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="list-group text-center"> <%= link_to "サインアウト", destroy_user_session_path, method: :delete, class: "list-group-item list-group-item-action" %> <%= link_to "キャンセル", "#", class: "list-group-item list-group-item-action", "data-dismiss": "modal" %> </div> </div> </div> </div> </div> <% if @user == current_user %> <div class="row"> <p> <%= @user.email %> </p> </div> <% end %> </div> </div> </div> ``` コード ``` ```class UsersController < ApplicationController def show @user = User.find_by(id: params[:id]) @users= User.all end def follow(id: post_id) follower.create(followed_id: post_id) end # ユーザーのフォローを外す  def unfollow(id: post_id) follower.find_by(followed_id: post_id).destroy  end # フォローしていればtrueを返す  def following?(user) following_user.include?(user)  end end コード ``` ```Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'registrations' } root 'posts#index' get '/users/:id', to: 'users#show', as: 'user' post 'follow/:id' => 'relationships#follow', as: 'follow' # フォローする post 'unfollow/:id' => 'relationships#unfollow', as: 'unfollow' # フォロー外す resources :posts, only: %i(index new create show destroy) do resources :photos, only: %i(create) resources :likes, only: %i(create destroy) resources :users, only: :show # ここに追加する resources :comments, only: %i(create destroy) end end ``` ![イメージ説明](6b63a985506c09df43396ae95d004921.png) ### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

def follow(id: post_id)
とありますが、method定義の引数listにdefaultを定義する場合は定数とか計算結果が確定する式でなければなりません。post_idはここで突然出てきているのでエラーになっています。

このmethodはfollow_path(user.id)を実行するactionだと思いますが、ここで送り出されるuser.idはmethod実行の引数にはなりません。
paramsで渡されます。多分 params["id"]です。

他のactionとなるmethodも同じくです。

投稿2020/05/07 21:49

winterboum

総合スコア23347

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問