解決したいこと
ストロングパラメーターの中身に指定したものを全て取得したい
##現状
目標管理アプリを製作中です。(利用環境は、「rails 5.2.3」「ruby 2.5.1」)
まず、新規作成ページから作成しました。このページで入力した値をコントローラーで取得しようとストロングパラメーターを設定したのですが、作成を実行すると、以下のエラーが発生しcreateアクションが正しく実行しませんでした。
「undefined local variable or method `target' for #TargetsController:0x00007f94aa1117c8 Did you mean? target_url」
ストロングパラメーター定義部分がエラー該当箇所なのですがどこかおかしな点はありますか?
ちなみにform_withを使っています。
データベース(モデル)はTargetモデル(目標の情報を入っている)、Categoryモデル(カテゴリの情報が入っている)、Priorityモデル(優先度の情報が入っている)の3つがあります。
※関係あるかわかりませんが、Bootstrapも使用してます。
エラー全文
ruby
1NameError in TargetsController#create 2undefined local variable or method `target' for #<TargetsController:0x00007f94aa1117c8> Did you mean? target_url 3Extracted source (around line #42): 440 541 642 743 844 945 10 11 private 12 def target_params 13 params.require(:target).permit(:title, :price, :date, :memo, :created_at).merge(category_id: target.category.id,priority: target.priority.id) 14 end 15 16 def move_to_index 17 18Rails.root: /Users/ren0826nosuke/Desktop/projects/target-app 19 20Application Trace | Framework Trace | Full Trace 21app/controllers/targets_controller.rb:42:in `target_params' 22app/controllers/targets_controller.rb:20:in `create' 23Request 24Parameters: 25 26{"utf8"=>"✓", 27 "authenticity_token"=>"F5MW8QuNuGxrebg66IJL2TkYLX233VnhbnpqO1cvrFH05j6rkv8bN+GmxtrEJBWAKtIzHbtyzSVkACcDi6NBng==", 28 "target"=> 29 {"title"=>"aaaaa", 30 "category_id"=>"1", 31 "price"=>"100", 32 "date(1i)"=>"2020", 33 "date(2i)"=>"4", 34 "date(3i)"=>"14", 35 "memo"=>"", 36 "priority"=>"1", 37 "created_at(1i)"=>"2020", 38 "created_at(2i)"=>"4", 39 "created_at(3i)"=>"14"}, 40 "commit"=>"作成"} 41Toggle session dump 42Toggle env dump 43Response 44Headers: 45 46None
新たに出たエラー全文
ruby
1NoMethodError in Targets#create 2Showing /Users/ren0826nosuke/Desktop/projects/target-app/app/views/targets/new.html.erb where line #10 raised: 3 4undefined method `map' for nil:NilClass 5Extracted source (around line #10): 68 79 810 911 1012 1113 12 13 <div class="form-group"> 14 <%= form.label :category_id, "カテゴリ" %><br> 15 <%= form.select :category_id, @category.map{|category|[category.name,category.id]} %> 16 #上記selectの部分がエラー該当箇所です。 17 </div> 18 19 <div class="form-group"> 20 21Rails.root: /Users/ren0826nosuke/Desktop/projects/target-app 22 23Application Trace | Framework Trace | Full Trace 24app/views/targets/new.html.erb:10:in `block in _app_views_targets_new_html_erb__2269560302185157887_70138228332500' 25app/views/targets/new.html.erb:2:in `_app_views_targets_new_html_erb__2269560302185157887_70138228332500' 26app/controllers/targets_controller.rb:24:in `create' 27Request 28Parameters: 29 30{"utf8"=>"✓", 31 "authenticity_token"=>"F5MW8QuNuGxrebg66IJL2TkYLX233VnhbnpqO1cvrFH05j6rkv8bN+GmxtrEJBWAKtIzHbtyzSVkACcDi6NBng==", 32 "target"=> 33 {"title"=>"aaaaa", 34 "category_id"=>"1", 35 "price"=>"100", 36 "date(1i)"=>"2020", 37 "date(2i)"=>"4", 38 "date(3i)"=>"14", 39 "memo"=>"", 40 "priority"=>"1", 41 "created_at(1i)"=>"2020", 42 "created_at(2i)"=>"4", 43 "created_at(3i)"=>"14"}, 44 "commit"=>"作成"} 45Toggle session dump 46Toggle env dump 47Response 48Headers: 49 50None
試してみた事
createアクションにbinding.pryでparamsの中身を見てみると以下の通りでした。
:category_id, :priorityが許可されていないと出ていますがなぜなのかはわかっていません。。
ruby
1 2[1] pry(#<TargetsController>)> params 3=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"F5MW8QuNuGxrebg66IJL2TkYLX233VnhbnpqO1cvrFH05j6rkv8bN+GmxtrEJBWAKtIzHbtyzSVkACcDi6NBng==", "target"=>{"title"=>"aaaaa", "category_id"=>"1", "price"=>"100", "date(1i)"=>"2020", "date(2i)"=>"4", "date(3i)"=>"14", "memo"=>"", "priority"=>"1", "created_at(1i)"=>"2020", "created_at(2i)"=>"4", "created_at(3i)"=>"14"}, "commit"=>"作成", "controller"=>"targets", "action"=>"create"} permitted: false> 4[2] pry(#<TargetsController>)> target_params 5Unpermitted parameters: :category_id, :priority 6NameError: undefined local variable or method `target' for #<TargetsController:0x00007f94aa1117c8> 7Did you mean? target_url 8from /Users/ren0826nosuke/Desktop/projects/target-app/app/controllers/targets_controller.rb:42:in `target_params'
記述コード
コントローラーの詳細は以下の通りです。
targets_controller.rb
ruby
1class TargetsController < ApplicationController 2 # before_action :move_to_index, only: [:new] 3 4 def index 5 6 end 7 8 def new 9 @target=Target.new 10 @category=Category.all 11 @priority=Priority.all 12 end 13 14 def show 15 16 end 17 18 def create 19 binding.pry 20 @target = Target.create(target_params) 21 if @target.save 22 redirect_to "/" 23 else 24 render :new 25 end 26 end 27 28 def edit 29 30 end 31 32 def update 33 34 end 35 36 def delete 37 38 end 39 40 private 41 def target_params 42 params.require(:target).permit(:title, :price, :date, :memo, :created_at).merge(category_id: target.category.id,priority: target.priority.id) 43 # 上記がエラー該当箇所です 44 end 45 46 def move_to_index 47 redirect_to "/" 48 end 49end
新規作成ページの詳細は以下の通りです。
new.html.erb
ruby
1<div class="container"> 2 <%= form_with model: @target, local:true do |form| %> 3 <div class="form-group"> 4 <%= form.label :title , "タイトル" %> 5 <%=form.text_field :title ,class:"form-control",placeholder:"タイトル" %> 6 </div> 7 8 <div class="form-group"> 9 <%= form.label :category_id, "カテゴリ" %><br> 10 <%= form.select :category_id, @category.map{|category|[category.name,category.id]} %> 11 </div> 12 13 <div class="form-group"> 14 <%= form.label :price , "予算・価格" %> 15 <%=form.text_field :price ,class:"form-control", placeholder:"¥" %> 16 </div> 17 18 <div class="form-group"> 19 <%= form.label :date , "期日" %><br> 20 <%=form.date_select( 21 :date, 22 use_month_numbers: true, 23 start_year: 1998, 24 end_year: (Time.now.year + 100), 25 class:"form-control" 26 )%> 27 </div> 28 29 <div class="form-group"> 30 <%= form.label :memo , "メモ" %> 31 <%=form.text_area :memo ,class:"form-control",placeholder:"メモ" %> 32 </div> 33 34 <div class="form-group"> 35 <%= form.label :priority, "優先度" %><br> 36 <%= form.select :priority, @priority.map{|priority|[priority.name,priority.id]} %> 37 </div> 38 39 <div class="form-group"> 40 <%= form.label :created_at , "作成日" %><br> 41 <%=form.date_select( 42 :created_at, 43 use_month_numbers: true, 44 start_year: 1998, 45 end_year: (Time.now.year + 100), 46 class:"form-control" 47 )%> 48 </div> 49 50 <%=form.submit "作成" ,class:"btn btn-primary btn-lg active"%> 51 52 <% end %> 53</div> 54
モデルの詳細は以下の通りです。
category.rb
ruby
1class Category < ApplicationRecord 2 has_many :targets 3end 4
priority.rb
ruby
1class Priority < ApplicationRecord 2 has_many :targets 3end 4
targets.rb
ruby
1class Target < ApplicationRecord 2 belongs_to :category 3 belongs_to :priority 4 5 validates :title, presence: true 6 validates :price, numericality: { only_integer: true },presence: true 7end 8
一応schema.rbも載せておきます。
schema.rb
ruby
1# This file is auto-generated from the current state of the database. Instead 2# of editing this file, please use the migrations feature of Active Record to 3# incrementally modify your database, and then regenerate this schema definition. 4# 5# Note that this schema.rb definition is the authoritative source for your 6# database schema. If you need to create the application database on another 7# system, you should be using db:schema:load, not running all the migrations 8# from scratch. The latter is a flawed and unsustainable approach (the more migrations 9# you'll amass, the slower it'll run and the greater likelihood for issues). 10# 11# It's strongly recommended that you check this file into your version control system. 12 13ActiveRecord::Schema.define(version: 2020_04_14_040458) do 14 15 create_table "categories", force: :cascade do |t| 16 t.string "name" 17 t.datetime "created_at", null: false 18 t.datetime "updated_at", null: false 19 end 20 21 create_table "priorities", force: :cascade do |t| 22 t.string "name" 23 t.datetime "created_at", null: false 24 t.datetime "updated_at", null: false 25 end 26 27 create_table "targets", force: :cascade do |t| 28 t.string "title" 29 t.text "memo" 30 t.date "date" 31 t.integer "category_id" 32 t.integer "priority_id" 33 t.datetime "created_at", null: false 34 t.datetime "updated_at", null: false 35 t.integer "price" 36 end 37 38end 39
回答2件
あなたの回答
tips
プレビュー