前提・実現したいこと
語学学習の先生・生徒のマンツーマンレッスンのマッチングサイトを作っています。
登録されている先生のindexページで、生徒がメッセージのリクエストを送信するという機能を実装したいのですが、エラーが出てうまくいきません。
発生している問題・エラーメッセージ
ActionController::ParameterMissing in RequestsController#create param is missing or the value is empty: request Parameters: {"utf8"=>"✓", "authenticity_token"=>"/24q3AE8dRvzf6iuEyRibLQirvu9jfzX+6Efq+ThMLLHFiRzUFUjHcvFvffC6xwGG129ZsmcTe+8Ye/onjI0PQ==", "commit"=>"メッセージリクエスト", "teacher_id"=>"4"}
該当のソースコード
teachers_controller
Ruby
1class TeachersController < ApplicationController 2 3 def index 4 @request = Request.new 5 @user = current_user 6 @teachers = Teacher.all 7 @student = @user.student 8 end 9 private 10 11 def teacher_params 12 params.require(:teacher).permit(:user_id, :budget, :japanese_level, :first_language, :message) 13 end 14 15end
requests_controller
Ruby
1class RequestsController < ApplicationController 2 def create 3 @request = Request.new(requests_params) 4 @request.student_id = params[:student_id] 5 @request.save 6 end 7 8 private 9 def requests_params 10 params.require(:request).permit(:teacher_id, :student_id, :status) 11 end 12end 13
index.html.erb(teacher)
Ruby
1<% @teachers.each do |t| %> 2 <%= attachment_image_tag t.user,:image, class:"index_img", fallback: "No_img.jpg" %> 3 <table> 4 <tr> 5 <td><div class="fa fa-user"><%= t.user.nickname %>--<%= t.user.age %></td> 6 </tr> 7 <tr> 8 <td><div class="fa fa-comment"><%= t.message %></td> 9 </tr> 10 <tr> 11 <td><%= form_for(@request, url: teacher_requests_path(t.id), method: :post) do |f| %> 12 <% f.hidden_field :teacher_id, :value => t.id %> 13 <% f.hidden_field :student_id, :value => @student.id %> 14 <% f.hidden_field :status, :value => 0 %> 15 <%= f.submit "メッセージリクエスト", class:"btn btn-default" %> 16 <% end %> 17 </td> 18 </tr> 19 </table> 20 <% end %>
routes.rb
Ruby
1Rails.application.routes.draw do 2 get 'rooms/show' 3 4 devise_for :users 5 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 6 root 'tops#top' 7 get '/rules' => 'tops#rules' 8 9 resources :users do 10 resources :teachers, shallow: true, except: [:index] do 11 resource :favorites, only: [:create, :destroy] 12 resources :requests 13 end 14 resources :students, shallow: true, except: [:index] do 15 resource :favorites, only: [:create, :destroy] 16 resources :requests 17 end 18 resources :rooms, only: [:index] 19 end 20 21 resources :rooms, except: [:index] do 22 resources :messages, shallow: true, except: [:edit] 23 end 24 resources :students, only: [:index] 25 resources :teachers, only: [:index] 26 27end
teacher.rb
class Teacher < ApplicationRecord belongs_to :user has_many :requests, through: :student enum japanese_level: {話せない: 0,日常会話レベル: 1,ビジネスレベル: 2,ネイティブレベル: 3} end
student.rb
class Student < ApplicationRecord belongs_to :user has_many :teachers, through: :requests enum japanese_level: {話せない: 0,日常会話レベル: 1,ビジネスレベル: 2,ネイティブレベル: 3} end
request.rb
class Request < ApplicationRecord belongs_to :teacher belongs_to :student enum status: {新規リクエスト: 0, 承認済み: 1, 拒否: 2} end
試したこと
エラーの内容から、requestで受け取りたいパラメータが渡ってきていないことは分かるのですが、解消の仕方がわかりません。
ちなみに、hidden_fieldを使わずにnumber_fieldでデータを送るとエラーにならず、 "request"=>{"student_id"=>"1", "status"=>"0"}という結果になります。requestのパラメータが{ }で囲まれているのが何かヒントになりそうなのですが検索してもうまくヒットしません。
補足情報(FW/ツールのバージョンなど)
初心者で、始めて質問させていただきます。抜けている点などありましたらご指摘ください。
よろしくおねがいいたします。
開発環境
ruby 2.3.6
Rails 5.1.6
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。