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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

Q&A

解決済

1回答

2729閲覧

undefined method `id' for "数値":String

SomaSekimoto

総合スコア9

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

0グッド

2クリップ

投稿2020/03/15 09:58

編集2020/03/15 11:07

前提・実現したいこと

Ruby 2.5.1
Rails 5.2.4.1
JQuery 4.3.5
active_hash 3.1.0
ancestry 3.0.7

某フリマアプリを模したアプリを開発中なのですが、出品機能で、商品情報をデータベースに登録をしたい。

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

submitボタンを押すと、

error

1 NoMethodError in ItemsController#create 2 undefined method `id' for "6":String 3 4=> @item = Item.new(item_params) 5 6 if @item.save! 7 8 app/controllers/items_controller.rb:68:in `create' 9 10

この"6"というのは、paramsの中のconditionの値なのですが、integer型で保存されるはずが、string型になっているし、そもそもid というメソッドはcondition には使っていないし、他のfieldにも使っていないはず。。

該当のソースコード

paramsのimagesの記述は割愛

parameters

1Parameters: 2 3{"utf8"=>"✓", 4 "authenticity_token"=>"6EPm8+xm+3boKu3Zx4u3eooAgiUncilmAVXHTnGu6HTk6HnSpJoo7JvcNxgedr83SVz1biocFprLjYFJX/ZWzg==", 5 "item"=> 6 {"images_attributes"=> 7 {"0"=> 8 {"content"=> 9 #<ActionDispatch::Http::UploadedFile:0x00007fc25a0625d8 10 @content_type="image/jpeg", 11 @headers= 12 "Content-Disposition: form-data; name=\"item[images_attributes][0][content]\"; filename=\"\xE3\x82\xBF\xE3\x82\x99\xE3\x82\xA6\xE3\x83\xB3\xE3\x83\xAD\xE3\x83\xBC\xE3\x83\x88\xE3\x82\x99 (1).jpeg\"\r\n" + 13 "Content-Type: image/jpeg\r\n", 14 @original_filename="ダウンロード (1).jpeg", 15 @tempfile=#<File:/var/folders/mk/8f7vv9q95xdf64s6pz1dk_vr0000gn/T/RackMultipart20200315-58575-1ujrxyn.jpeg>>}}, 16 "name"=>"aaaa", 17 "description"=>"bbbbb", 18 "parent_name"=>"レディース", 19 "brand_id"=>"3", 20 "condition"=>"6", 21 "fee"=>"1", 22 "shipping_days"=>"1", 23 "area"=>"秋田県", 24 "price"=>"5000"}, 25 "category_id"=>"21", 26 "commit"=>"出品する"}

ItemParams

1<ActionController::Parameters {"name"=>"hhhh", "description"=>"gagagga", 2"condition"=>"6", "price"=>"4444", "fee"=>"1", "brand_id"=>"1", "area"=>"青森県", 3 "shipping_days"=>"1", "images_attributes"=>省略,,, permitted: true>} permitted: true>, 4"user_id"=>1, "category_id"=>"101"} permitted: true>

items_controller.rb

ruby

1def new 2 @item = Item.new 3 @brands = Brand.all 4 @category_parent_array = ["指定なし"] 5 Category.where(ancestry: nil).each do |parent| 6 @category_parent_array << parent.name 7 end 8 @item.images.build 9 end 10 11 def create 12 @item = Item.new(item_params) 13 if @item.save! 14 @image = @item.images.create 15 redirect_to :root 16 else 17 render :new 18 end 19 end 20 21private 22 def item_params 23 params.require(:item).permit( 24 :name, :description, :condition, :price, 25 :fee, :brand_id, :area, :shipping_days, 26 images_attributes: [:content, :id, :_destroy] 27 ).merge(user_id: current_user.id, category_id: params[:category_id], brand_id: params[:item][:brand_id]) 28end

_form.html.haml

ruby

1.wrapper.name-wrapper 2 %label.wrapper__label 3 商品名: 4 %span.required 5 ※必須 6 = f.text_field :name, placeholder: "40字まで", class: "name-wrapper__name form-control", required: "required" 7 .wrapper.description-wrapper 8 %label.wrapper__label 9 商品説明: 10 %span.required 11 ※必須 12 = f.text_area :description, placaeholder: "色・素材・重さ・定価・注意点などを書きましょう(1000文字まで)", class: "name-wrapper__description form-control", required: "required", size: "40 x 10" 13 .form-separator 14 15 .wrapper.category-wrapper 16 = f.label :category_id , class: 'wrapper__label category-wrapper-label', id: "wrapper__label--category" do 17 カテゴリー: 18 %span.required ※必須 19 .category-wrapper-box 20 .category-wrapper-select 21 .category-wrapper-select__box 22 = f.select :parent_name, @category_parent_array, {}, {class: 'category-wrapper__category form-control', id: 'parent_category'} 23 24 25 .wrapper.brand-wrapper 26 %label.wrapper__label 27 ブランド: 28 = f.select :brand_id, options_for_select(@brands.map{|b| [b.name, b.id, {}]}), {prompt: "指定なし"}, class: "brand-wrapper__brand form-control" 29 30 .wrapper.condition-wrapper 31 %label.wrapper__label 32 商品の状態: 33 %span.required 34 ※必須 35 = f.collection_select :condition, Condition.all, :id, :name, {selected: 1}, {class: "condition-wrapper__condition form-control", required: "required"} 36 .form-separator 37 38 .wrapper.fee-wrapper 39 %label.wrapper__label 40 配送料の負担: 41 %span.required 42 ※必須 43 = f.collection_select :fee, Fee.all, :id, :name, {selected: 1}, {class: "fee-wrapper__fee form-control", required: "required"} 44 45 .wrapper.shipping_days-wrapper 46 %label.wrapper__label 47 発送日の目安: 48 %span.required 49 ※必須 50 = f.collection_select :shipping_days, ShippingDay.all, :id, :name, {selected: 1}, {class: "shipping_days-wrapper__shipping-days form-control", required: "required"} 51 .wrapper.area-wrapper 52 %label.wrapper__label 53 発送元の地域: 54 %span.required 55 ※必須 56 = f.collection_select :area, Prefecture.all, :name, :name, {selected: 1}, {class: "area-wrapper__area form-control", required: "required"} 57 .form-separator 58 59 .wrapper.price-wrapper 60 %label.wrapper__label 61 価格: 62 %span.required 63 ※必須 64 = f.number_field :price, class: "price-wrapper__price form-control", required: "required" 65 66 .wrapper.submit-wrapper 67 = f.submit "出品する", class: "btn submit-wrapper__submit-btn"

item.rb

ruby

1class Item < ApplicationRecord 2 belongs_to :brand, optional: true 3 belongs_to :user, optional: true 4 belongs_to :category, optional: true 5 has_many :images, dependent: :destroy 6 accepts_nested_attributes_for :images, allow_destroy: true 7 8 extend ActiveHash::Associations::ActiveRecordExtensions 9 belongs_to_active_hash :prefecture 10 belongs_to_active_hash :fee 11 belongs_to_active_hash :shipping_day 12 belongs_to_active_hash :condition 13end

condition.rb

ruby

1class Condition < ActiveHash::Base 2 self.data = [ 3 { id: 1, name: '新品、未使用'}, {id: 2, name: '未使用に近い'}, {id: 3, name: '目立った傷や汚れなし'}, 4 { id: 4, name: 'やや傷や汚れあり'}, {id: 5, name: '傷や汚れあり'}, {id: 6, name: '全体的に状態が悪い'} 5 ] 6end

試したこと

ビューファイルでのconditionのフォームのvalue属性の確認をしたが、数字になっている。
Conditionモデルも記述ミスもない。
もしかしたら、paramsの中のparent_nameの値が、邪魔しているのかな?

補足情報(FW/ツールのバージョンなど)

condition・prefecture・fee・shipping_dayなどは、active_hashをitemモデルに紐付けて使っています。

categoryのselectタグは、jqueryで動的に出てくるようにしています。

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

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

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

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

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

winterboum

2020/03/15 10:45

エラーメッセージを全文載せてください
guest

回答1

0

ベストアンサー

まず 「string型になっているし」 ですが、paramsの値はみな string です。
newなりcreateなりで渡された先で、columの型に応じて変換されます。
ので、confitinがIntegerなら "6" が 6 に変換されて格納されます。
でも
condition は belongs_to_active_hash で ActiveHash ですね。んで id を持っています。
Itemのmigrationがないのですが多分 condition を integerで定義していると思います。ではなく condition_id を定義しなければなりません。

投稿2020/03/15 14:35

winterboum

総合スコア23333

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

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

SomaSekimoto

2020/03/16 02:56

返信ありがとうございます。 paramsの値が全てstringだということは知りませんでした。 となると、feeカラムもshipping_daysカラムも名前変更しなければいけないということですね。 ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問