###前提・実現したいこと
ユーザがフォームからPOSTしたデータをコントローラのインスタンス変数で受け取りたい。
Railsでアプリを作っています。
単純にフォームから送信したデータをコントローラで受け取りたいのですが、ParameterMissingが出て困っています。
フォーム入力データはしっかりとcreateアクションに到達しており、かつform_forの第一引数でモデルを指定しているのに
何故パラメータが空だと言われるのかが分かりません。
宜しくお願いします。
###発生している問題・エラーメッセージ
ActionController::ParameterMissing in DynamicPagesController#create param is missing or the value is empty: item Extracted source (around line #25): 23 24 def item_params 25 params.require(:item).permit(:name, :description, :money, 26 :method, :deadline, :comment) 27 end 28 end Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"hogehoge", "name"=>"a", "description"=>"i", "money"=>"6", "method"=>"", "deadline"=>"1", "comment"=>"test"}
###該当のソースコード
app/views/dynamic_pages/new.html.erb
Ruby
1<%= form_for(@item, url: item_register_path, html: {class: 'form-horizontal'}) do |f| %> 2 3 <!-- name --> 4 <div class="col-sm-5"> 5 <input type="text" name="name" class="form-control" required aria-required="true"> 6 </div> 7 8 9 <!-- description --> 10 <div class="col-md-5"> 11 <input type="text" name="description" class="form-control"> 12 </div> 13 14 <!-- money --> 15 <div class="col-md-5"> 16 <input type="number" name="money" class="form-control" required aria-required="true"> 17 </div> 18 19 <!-- method --> 20 <div class="col-md-5"> 21 <select name="method" class="form-control"> 22 <option value="" selected>選択してください</option> 23 <option value="sample">sample</option> 24 </select> 25 </div> 26 27 <!-- deadline --> 28 <div class="col-md-5"> 29 <input type="number" name="deadline" class="form-control" min="0" max="31"> 30 </div> 31 32 <!-- comment --> 33 <div class="col-md-5"> 34 <textarea class="form-control" name="comment" rows="3"></textarea> 35 </div> 36 37 <!-- submit --> 38 <div class="col-md-offset-9"> 39 <button type="submit" class="btn btn-primary">登録</button> 40 </div> 41 42<% end %>
config/routes.rb
Ruby
1Rails.application.routes.draw do 2 3 get '/item-register', to: 'dynamic_pages#new' 4 post '/item-register', to: 'dynamic_pages#create' 5 resources :items 6 7end
DynamicPagesController
Ruby
1class DynamicPagesController < ApplicationController 2 3 def new 4 @item = Item.new 5 end 6 7 def create 8 @item = Item.new(item_params) 9 if @item.save 10 11 else 12 13 end 14 end 15 16 private 17 18 def item_params 19 params.require(:item).permit(:name, :description, :money, 20 :method, :deadline, :comment) 21 end 22end
###試したこと
@item = Item.new(item_params)を@item = paramsに書き換えるとうまくいきますが、
ストロングパラメータを使っていないのにエラーが出ないのも意味が分かりません。
セキュリティを考慮して、想定外のキーを受け取らない実装にしたいです。
###補足情報(言語/FW/ツール等のバージョンなど)
Ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
Rails 5.0.0.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/02 14:16
2017/07/02 14:20