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

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

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

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

jQuery

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

Q&A

解決済

1回答

1719閲覧

Rails5 自動住所に入力機能が機能しない(jquery.jpostal.js)

shimoner

総合スコア3

Ruby on Rails

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

jQuery

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

0グッド

0クリップ

投稿2020/07/05 15:55

自動住所に入力機能の時に「Uncaught TypeError: Cannot read property 'tagName' of undefined」エラーがでます、、
症状スクショ↓
イメージ説明

参考にした記事はこちらです[https://hajimeteblog.com/jpostal/#toc11]

全角なども探しましたが見つかりませんでした。
改善にご協力お願い致します。
devise導入済み
jquery.jpostal.jsファイルもjavascripts内にあり

javascripts/application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's // vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require rails-ujs //= require activestorage //= require turbolinks //= require jquery //= require bootstrap-sprockets //= require_tree . //= require jquery.jpostal

javascripts/users.coffee

$ -> $("#address_zipcode").jpostal({ # 郵便番号の入力欄が1つの場合 # 111-1111と1111111のどちらの入力形式でも住所を自動入力してくれる postcode : [ "#address_zipcode" ], # 郵便番号の入力欄が3桁-4桁で分かれている場合 # postcode : [ '#zipcode1', '#zipcode2' ] # 入力項目フォーマット # %3 都道府県 # %4 市区町村 # %5 町域 # %6 大口事業所の番地 # %7 大口事業所の名称 address : { "#address_prefecture_name" : "%3", # 都道府県が入力される "#address_city" : "%4%5", # 市区町村と町域が入力される "#address_street" : "%6%7" # 大口事務所の番地と名称が入力される } })

view/device/registration/new.html.erb

<h2>Sign up</h2> <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <!--name表示の追加--> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name, autofocus: true, autocomplete: "name" %> </div> <!--name表示の追加--> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true, class: "email" %> </div> <!-- 住所登録機 --> <div class="field"> <%= f.label :postal_code %><br /> <%= f.text_field :postal_code, autofocus: true, class: "postal_code",id: "address_zipcode" %> </div> <div class="field"> <%= f.label :prefecture_code %><br> <!-- <%= f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :code, :name, class: "address", id: "address_prefecture_name" %> <!--下記へ変高 プルダウンの属性がcodeになっている、jpostal.jsと都道府県名で同期させることがでないnameへ--> <%= f.collection_select :prefecture_code, JpPrefecture::Prefecture.all, :name, :name, class: "address" %> <%= f.label :city %><br> <%= f.text_field :city, autofocus: true, class: "address", id:"address_city" %><br> <%= f.label 'Street' %><br> <%= f.text_field :building, autofocus: true, class: "address", id: "address_street" %> </div> <!-- 住所登録機能 --> <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "new-password", class: "password" %> </div> <div class="field"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password", class: "password_confirm" %> </div> <div class="actions"> <%= f.submit "Sign up",class: "btn btn-success" %> </div> <% end %> <%= render "devise/shared/links" %>

models/user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :books, dependent: :destroy has_many :favorites, dependent: :destroy has_many :book_comments, dependent: :destroy attachment :profile_image #フォロー機能 has_many :relationships #has_many :relationships, foreign_key: 'user_id'と一緒の意味 has_many :followings, through: :relationships, source: :follow #フォローしているユーザー達を取得 has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' #フォローされているユーザー達を取得 has_many :followers, through: :reverse_of_relationships, source: :user #バリデーションは該当するモデルに設定する。エラーにする条件を設定できる。 validates :name, presence: true validates :name, length: {maximum: 20, minimum: 2} validates :introduction,length:{maximum: 50} def follow(other_user) unless self == other_user self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship end def following?(other_user) self.followings.include?(other_user) end def self.search(search,word) if search == "forward_match" @user = User.where("name LIKE?","#{word}%") elsif search == "backward_match" @user = User.where("name LIKE?","%#{word}") elsif search == "perfect_match" @user = User.where("#{word}") elsif search == "partial_match" @user = User.where("name LIKE?","%#{word}%") else @user = User.all end end #住所機能 include JpPrefecture jp_prefecture :prefecture_code #prefecture_codeはuserが持っているカラム #postal_codeからprefecture_nameに変換するメソッド def prefecture_name JpPrefecture::Prefecture.find(code: prefecture_code).try(:name) end def prefecture_name=(prefecture_name) self.prefecture_code = JpPrefecture::Prefecture.find(name: prefecture_name).code end end

Gemfile

source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.7' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.4', '>= 5.2.4.3' gem 'actionpack', '>= 5.2.4.3' gem 'activestorage', '>= 5.2.4.3' gem 'activesupport', '>= 5.2.4.3' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use Puma as the app server gem 'puma', '>= 3.12.6' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do gem 'capybara', '>= 2.15' gem 'rspec-rails' gem "factory_bot_rails" gem 'faker' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'devise' gem 'refile', require: 'refile/rails', github: 'manfe/refile' gem 'refile-mini_magick' gem 'jquery-rails' gem 'bootstrap-sass', '~> 3.3.6' gem 'jp_prefecture'

application_controller.rb

class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def after_sign_in_path_for(resource) user_path(current_user.id) end def after_sign_out_path_for(resource) root_path end def configure_permitted_parameters #devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email]) devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :profle_image, :email, :postal_code, :prefecture_code, :city, :building])#sign_up時の登録情報追加 devise_parameter_sanitizer.permit(:sign_in, keys: [:name]) # ログイン時はnameを使用 end end

shema.rb

class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def after_sign_in_path_for(resource) user_path(current_user.id) end def after_sign_out_path_for(resource) root_path end def configure_permitted_parameters #devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email]) devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :profle_image, :email, :postal_code, :prefecture_code, :city, :building])#sign_up時の登録情報追加 devise_parameter_sanitizer.permit(:sign_in, keys: [:name]) # ログイン時はnameを使用 end end

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

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

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

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

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

guest

回答1

0

自己解決

javascripts/users.coffeeの"#address_prefecture_name" を#user_prefecture_codeに改善したら治りました。

投稿2020/07/08 07:28

shimoner

総合スコア3

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問