rails4.2.5です。
evaluationsテーブルにフォームで値を送信しようと思っているのです
が、evaluations_controller.rbのstrong parametersを作成すると
param is missing or the value is empty: evaluations
というエラーが起きてしまいます。
コードは以下のとおりです。
evaluations_controller.rb
lang
1class EvaluationsController < ApplicationController 2 3 def evaluation 4 @teacher = Teacher.find_by(params[:teacher_id]) 5 @evaluations = current_student.evaluations.build(evaluation_params) 6 @evaluations.save 7 end 8 9 def index 10 @teacher = Teacher.find_by(params[:teacher_id]) 11 @evaluations_of_teacher = @teacher.evaluations.all 12 end 13 14 15private 16 17 def evaluation_params 18 params.require(:evaluations).permit( :title, :body, :teacher_id ) 19 end 20 21end 22
views/evaluations/evaluation.html.erb
<%= form_for @evaluations, url: evaluation_path, method: :post do |f| %> <p><%= f.text_field :title %></p> <p><%= f.text_area :body %></p> <%= f.hidden_field :teacher_id, :value => @teacher.id %> <%= f.submit %> <% end %>
schemafile
lang
1create_table "evaluations", force: :cascade do |t| 2 t.integer "teacher_id", default: "" 3 t.integer "student_id" 4 t.string "title", default: "" 5 t.text "body" 6 7 t.datetime "created_at", null: false 8 t.datetime "updated_at", null: false 9end
routes.rb
lang
1Rails.application.routes.draw do 2 root 'home#index' 3 4 devise_for :teachers, :controllers => { 5 :sessions => "teachers/sessions", 6 :registrations => "teachers/registrations", 7 :passwords => "teachers/passwords", 8 # :omniauth_callbacks => "teachers/omniauth_callbacks" 9 } 10 11 12 13 devise_for :students, :controllers => { 14 :sessions => "students/sessions", 15 :registrations => "students/registrations", 16 :passwords => "students/passwords", 17 # :omniauth_callbacks => "students/omniauth_callbacks" 18 } 19 20 21 resources :teachers, only: [:show, :index] do 22 resources :evaluations, only: [:index] 23 end 24 25 get 'evaluation' => evaluations#evaluation', as:'evaluation' 26 post 'evaluation' => 'evaluations#evaluation' 27 delete 'unevaluation/:teacher_id' => 'evaluations#unevaluation' 28 29 30 # The priority is based upon order of creation: first created -> highest priori 31ty. 32 # See how all your routes lay out with "rake routes". 33 34 # You can have the root of your site routed with "root" 35 # root 'welcome#index' 36 37 # Example of regular route: 38 # get 'products/:id' => 'catalog#view' 39 40 # Example of named route that can be invoked with purchase_url(id: product.id) 41 # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase 42 43 # Example resource route (maps HTTP verbs to controller actions automatically): 44 # resources :products 45 46 # Example resource route with options: 47 # resources :products do 48 # member do 49 # get 'short' 50 # post 'toggle' 51 # end 52 # 53 # collection do 54 # get 'sold' 55 # end 56 # end 57 58 # Example resource route with sub-resources: 59 # resources :products do 60 # resources :comments, :sales 61 # resource :seller 62 # end 63 64 # Example resource route with more complex sub-resources: 65 # resources :products do 66 # resources :comments 67 # resources :sales do 68 # get 'recent', on: :collection 69 # end 70 # end 71 72 # Example resource route with concerns: 73 # concern :toggleable do 74 # post 'toggle' 75 # end 76 # resources :posts, concerns: :toggleable 77 # resources :photos, concerns: :toggleable 78 79 # Example resource route within a namespace: 80 # namespace :admin do 81 # # Directs /admin/products/* to Admin::ProductsController 82 # # (app/controllers/admin/products_controller.rb) 83 # resources :products 84 # end 85end 86
strong parametersの記述をコメントアウトすると、
strong parametersを必要としない値はちゃんとevaluationsテーブルに保存されるのに、
strong parametersの記述をすると、
ActionController::ParameterMissing (param is missing or the value is empty: evaluations):
というエラーが表示されます。
たくさんググっても解決策が見当たらなかったので分かる方教えて欲しいです。
ググったサイト
http://nograve.hatenadiary.jp/entry/2015/11/05/161948
回答2件
あなたの回答
tips
プレビュー