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

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

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

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

jQuery

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

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

Q&A

解決済

2回答

1625閲覧

多階層カテゴリーを実現したい

unchan_net

総合スコア3

Ruby on Rails

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

jQuery

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

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

0グッド

0クリップ

投稿2020/08/30 10:54

Railsで多階層カテゴリーを実装したいと思っております。

都道府県を選択すると、それと関連性のある市区町村が表示される機能です。
(例:茨城県 => つくば市)

自分で一から制作するほどのスキルはないため、下記URLを参考にしながら実装を進めていたところ躓きました。

http://kawahiro.hatenablog.jp/entry/2013/10/26/233051

出ているエラーとしましては、「ArgumentError」で、

cities

1<%= collection_select :city_id, City.where(prefecture_id: prefecture_id), :id, :name %>

恐らく、こちらの記述の部分がおかしいと思うのですが、原因がわかりません。

念の為、関係のありそうな周辺ファイルも添付しておきます。

[ _cities.html.erb ]

cities

1<div class="field"> 2<%= collection_select :city_id, City.where(prefecture_id: prefecture_id), :id, :name %> 3</div>

[routers.rb]

ruoters

1~省略 2 resources :microposts, only: [:create, :destroy] 3 resources :microposts do 4 collection do 5 get :cities_select 6 end 7 end 8end

[ _micropost_form.html.erb ]

micropost

1<%= form_for(@micropost) do |f| %> 2 <%= render 'shared/error_messages', object: f.object %> 3 <div class="field"> 4 <%= f.text_area :content, placeholder: "Compose new micropost..." %> 5 </div> 6 <div class="field"> 7 <%= f.label :prefecture_id,"都道府県" %> 8 <%= f.collection_select :prefecture_id, Prefecture.all, :id, :name, include_blank: "選択してください" %> 9 </div> 10 <div class="field"> 11 <%= f.label :city_id,"市区町村" %> 12 <%= render partial: 'cities', locals: {prefecture_id: Prefecture.first.id} %> 13 </div> 14 <%= f.submit "Post", class: "btn btn-primary" %> 15<% end %>

[ micropost.rb ]

class Micropost < ApplicationRecord belongs_to :user belongs_to :prefecture, optional: true belongs_to :city default_scope -> { order(created_at: :desc) } validates :user_id, presence: true validates :content, presence: true, length: { maximum: 1000 } end

[schema.rb]

schema

1ActiveRecord::Schema.define(version: 2020_08_29_105438) do 2 3 create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 4 t.integer "prefecture_id" 5 t.string "name" 6 t.datetime "created_at", null: false 7 t.datetime "updated_at", null: false 8 end 9 10 create_table "microposts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 11 t.text "content" 12 t.bigint "user_id" 13 t.datetime "created_at", null: false 14 t.datetime "updated_at", null: false 15 t.bigint "prefecture_id" 16 t.index ["prefecture_id"], name: "index_microposts_on_prefecture_id" 17 t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at" 18 t.index ["user_id"], name: "index_microposts_on_user_id" 19 end 20 21 create_table "prefectures", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", 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 "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 28 t.string "email", default: "", null: false 29 t.string "encrypted_password", default: "", null: false 30 t.string "reset_password_token" 31 t.datetime "reset_password_sent_at" 32 t.datetime "remember_created_at" 33 t.integer "sign_in_count", default: 0, null: false 34 t.datetime "current_sign_in_at" 35 t.datetime "last_sign_in_at" 36 t.string "current_sign_in_ip" 37 t.string "last_sign_in_ip" 38 t.string "confirmation_token" 39 t.datetime "confirmed_at" 40 t.datetime "confirmation_sent_at" 41 t.string "unconfirmed_email" 42 t.integer "failed_attempts", default: 0, null: false 43 t.string "unlock_token" 44 t.datetime "locked_at" 45 t.datetime "created_at", null: false 46 t.datetime "updated_at", null: false 47 t.string "provider" 48 t.string "uid" 49 t.string "username" 50 t.string "image" 51 t.text "profile" 52 t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true 53 t.index ["email"], name: "index_users_on_email", unique: true 54 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 55 t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true 56 end 57 58 add_foreign_key "microposts", "prefectures" 59 add_foreign_key "microposts", "users" 60end 61

[micropost.coffee]

$(document).on 'change', '#company_ms_pref', -> $.ajax( type: 'GET' url: '/microposts/cities_select' data: { ms_pref_id: $(this).val() } ).done (data) -> $('#cities_select').html(data)

[microposts_controller.rb]

~省略 def cities_select if request.xhr? render partial: 'cities', locals: {prefecture_id: params[:prefecture_id]} end end

ただ、どこを間違えているのかの見当がつかないあたり、自身のスキルが不足していることも重々承知しております。

ですので、エラーの解決法ではなく、「〜あたりがおかしい、〇〇についてもっと勉強すればわかる」等、抽象的なアドバイスでも、とても嬉しいです。

よろしくお願いします。

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

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

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

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

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

no1knows

2020/08/30 11:50

エラーの内容はすべて正確に記載してください。
unchan_net

2020/08/30 12:03

申し訳ありません。 ArgumentError in StaticPages#home Showing /app_name/app/views/static_pages/_cities.html.erb where line #2 raised: wrong number of arguments (given 4, expected 5..7) Extracted source (around line #2): 1 2 3 <div class="field"> <%= collection_select :city_id, City.where(prefecture_id: prefecture_id), :id, :name %> </div> Trace of template inclusion: app/views/shared/_micropost_form.html.erb, app/views/static_pages/home.html.erb Rails.root: /app_name Application Trace | Framework Trace | Full Trace app/views/static_pages/_cities.html.erb:2:in `_app_views_static_pages__cities_html_erb___1302765098415965002_69977113784340' app/views/shared/_micropost_form.html.erb:12:in `block in _app_views_shared__micropost_form_html_erb___54028724847143868_69977114408500' app/views/shared/_micropost_form.html.erb:1:in `_app_views_shared__micropost_form_html_erb___54028724847143868_69977114408500' app/views/static_pages/home.html.erb:8:in `_app_views_static_pages_home_html_erb__1402849096658922661_69977114476620' Request こちらになります。 よろしくお願いします。
unchan_net

2020/08/30 22:08

ありがとうございます。 こちらのページは見た事がなかったので、とても参考になりました。
guest

回答2

0

collection_select は 同名のmethodが2種あります。
ひとつは 引数が 4〜6、 f.collection_select
一つが 5〜7のものです。collection_select
unchan_net さんの _cities での使い方が 後者になっているのがエラーの原因です。

<%= render partial: 'cities', のlocals に f: f を加え
<%= f.collection_select :city_id,。。。 にしてください

投稿2020/08/30 22:42

編集2020/08/30 22:43
winterboum

総合スコア23329

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

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

unchan_net

2020/08/31 08:07

ありがとうございます!解決できました。
guest

0

ベストアンサー

ここのエラー分で_cities.html.erbの2行目のエラーとわかります。
エラー内容は、引数に4つ与えているが、必要(=期待)しているのは5~7個の引数という点です。

Showing /app_name/app/views/static_pages/_cities.html.erb where line #2 raised: wrong number of arguments (given 4, expected 5..7)

エラーが起きている場所はここです。

<%= collection_select :city_id, City.where(prefecture_id: prefecture_id), :id, :name %>

https://railsdoc.com/page/collection_select
ここにcollection_selectの使い方がかかれています。
ドキュメントに書いてあるように、引数の数を5個にしたのが以下です。

<%= collection_select :city_id, :name, City.where(prefecture_id: prefecture_id), :id, :name %>

投稿2020/08/30 15:33

hatsu

総合スコア1809

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

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

unchan_net

2020/08/31 08:08

ありがとうございます。 言われた通りに変更したところ、できました!
unchan_net

2020/08/31 08:10

また、エラーの見方までご教授していただき、誠にありがとうございます。 別の方の回答でも解決できたのですが、hatsuさんの方が早かったのと、少し詳しく書かれていたのでベストアンサーにさせて頂きました。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問