前提・実現したいこと
現在、railsにて顧客管理のアプリを作成しているのですが、
住所登録ページのデータが保存できません
何卒、よろしくお願いいたします
発生している問題・エラーメッセージ
search.html.erbのデータを保存する際 createアクションにを通らず、dbに保存ができません。 エラーメッセージはありません
該当のソースコード
search.html.erb
HTML
1<div class="container"> 2 <%= form_with url:search_searches_path, method: :get, local: true do |f| %> 3 <%= f.label :postal_code,"郵便番号検索" %> 4 <%= f.text_field :postal_code, class: "search_form",value:@zipcode, placeholder: "5420076" %> 5 <br> 6 <%= f.submit "検索する",class: "button is-black" %> 7 <% end %> 8 <%= form_with(model: @search, method: :get, local: true) do |f| %> 9 <%= f.label :postal_code,"郵便番号" %> 10 <%= f.text_field :postal_code, class: "search_form",value:@zipcode, placeholder: "5420076" %> 11 <%= f.label :address1,"都道府県" %> 12 <%= f.text_field :address1, class: "search_form",value:@address1, placeholder: "大阪府" %> 13 <%= f.label :address2,"市区町村" %> 14 <%= f.text_field :address2, class: "search_form",value:@address2, placeholder: "大阪市中央区" %> 15 <%= f.label :address3,"町域" %> 16 <%= f.text_field :address3, class: "search_form",value:@address3, placeholder: "難波 5-1-60" %> 17 <%= f.label :building_name,"建物名" %> 18 <%= f.text_field :building_name, class: "search_form",placeholder: "なんばスカイオ 15階" %> 19 <%= f.label :phone_number,"電話番号" %> 20 <%= f.text_field :phone_number, class: "search_form",placeholder: "050-3187-577" %> 21 <br> 22 <%= f.submit class: "button is-warning"%> 23 <% end %> 24</div> 25
routes.rb
ruby
1Rails.application.routes.draw do 2 get 'searches',to: "searches#index" 3 mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 4 devise_for :users, controllers: { 5 omniauth_callbacks: 'users/omniauth_callbacks', 6 registrations: 'users/registrations' 7 } 8 post '/callback' => 'linebot#callback' 9 get 'homes/index' 10 resources :homes, only: [:index] 11 root to: 'home#index' 12 resources :users 13 resources :attendances 14 resources :daytimes 15 resources :restaurants, only: [:index, :show] 16 resources :searches do 17 collection do 18 get 'search' 19 end 20 end 21 namespace :admin do 22 resources :restaurants, only: [:index, :new, :create, :show, :edit, :destroy] 23 end 24end 25 26
searches.controller.rb
ruby
1class SearchesController < ApplicationController 2 def show 3 @search = Search.find(params[:id]) 4 end 5 6 def index 7 @search = Search.all 8 end 9 10 def edit 11 @search = Search.find(params[:id]) 12 end 13 14 def update 15 @search = Search.find(params[:id]) 16 @search.update(update_params) 17 redirect_to root_path 18 end 19 20 21 def create 22 @search = Search.new(search_params) 23 @search.user_id = current_user.id 24 @search.save! 25 redirect_to root_path 26 end 27 28 def destroy 29 search = Search.find(params[:id]) 30 search.destroy 31 redirect_to root_path 32 end 33 34 def search 35 @search = Search.new 36 if postal_code = params[:postal_code] 37 params = URI.encode_www_form({zipcode: postal_code}) 38 uri = URI.parse("http://zipcloud.ibsnet.co.jp/api/search?#{params}") 39 response = Net::HTTP.get_response(uri) 40 result = JSON.parse(response.body) 41 if result["results"] 42 @zipcode = result["results"][0]["zipcode"] 43 @address1 = result["results"][0]["address1"] 44 @address2 = result["results"][0]["address2"] 45 @address3 = result["results"][0]["address3"] 46 end 47 end 48 end 49end 50 51private 52 def search_params 53 params.permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code) 54 end 55 56 def update_params 57 params.require(:search).permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code) 58 end
試したこと
rutesの get 'searches',to: "searches#index" を get 'searches',to: "searches#search"にし、 searches_controllersの@search = Search.newを削除すると保存ができますが、index.html.erbが開きません。
補足情報(FW/ツールのバージョンなど)
searches_controllersにbinding pryを記述しても止まりません
ruby '2.6.5'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/28 07:28
2020/10/28 07:58
2020/10/28 08:02