発生している問題・エラーメッセージ
updateアクションの値が変更されなく、エラーは出ておりません。 createは正常に動きます。
該当のソースコード
views/searches/edit.html.erb
html
1 <%= form_with(model: @search, local: true) do |f| %> 2 <%= f.label :postal_code,"郵便番号" %> 3 <%= f.text_field :postal_code, class: "search_form" %> 4 <%= f.label :address1,"都道府県" %> 5 <%= f.text_field :address1, class: "search_form" %> 6 <%= f.label :address2,"市区町村" %> 7 <%= f.text_field :address2, class: "search_form" %> 8 <%= f.label :address3,"町域" %> 9 <%= f.text_field :address3, class: "search_form" %> 10 <%= f.label :building_name,"建物名" %> 11 <%= f.text_field :building_name, class: "search_form" %> 12 <%= f.label :phone_number,"電話番号" %> 13 <%= f.text_field :phone_number, class: "search_form" %> 14 <br> 15 <%= f.submit class: "button is-warning"%> 16 <% end %>
config/routes.rb
ruby
1Rails.application.routes.draw do 2 get 'searches',to: "searches#search" 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 get 'homes/index' 9 resources :homes, only: [:index] 10 root to: 'home#index' 11 resources :users 12 resources :attendances 13 resources :daytimes 14 resources :restaurants, only: [:index, :show] 15 resources :searches do 16 collection do 17 get 'search' 18 end 19 end 20 namespace :admin do 21 resources :restaurants, only: [:index, :new, :create, :show, :edit, :destroy] 22 end 23end 24
controllers/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(search_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 search 29 if postal_code = params[:postal_code] 30 params = URI.encode_www_form({zipcode: postal_code}) 31 uri = URI.parse("http://zipcloud.ibsnet.co.jp/api/search?#{params}") 32 response = Net::HTTP.get_response(uri) 33 result = JSON.parse(response.body) 34 if result["results"] 35 @zipcode = result["results"][0]["zipcode"] 36 @address1 = result["results"][0]["address1"] 37 @address2 = result["results"][0]["address2"] 38 @address3 = result["results"][0]["address3"] 39 end 40 end 41 end 42end 43 44private 45 def search_params 46 params.permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code) 47 end
試したこと
updateアクションでbinding.pryをかけました
[2] pry(#<SearchesController>)> Search.find(params[:id])
CACHE Search Load (0.0ms) SELECT searches
.* FROM searches
WHERE searches
.id
= 1 LIMIT 1 [["id", 1], ["LIMIT", 1]]
↳ (pry):15:in `update'
=> #<Search:0x00007fd6cf3153f0
id: 1,
user_id: 3,
postal_code: "5420076",
address1: "大阪府",
address2: "大阪市中央区",
address3: "難波 5 -1 -60",
building_name: "なんば スカイオ 15階",
phone_number: "0503187-577",
created_at: Mon, 26 Oct 2020 11:19:35 JST +09:00,
updated_at: Mon, 26 Oct 2020 11:19:35 JST +09:00>
[3] pry(#<SearchesController>)> params
=> <ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"3C+utl44ydMFKy+o6uNfUVLEaegW9ryjuvwGKyhzN3jxAk8ZNJift1NaFmvrxWfYdhoeWpVxcMeZJJ1nvI2oHw==", "search"=>{"postal_code"=>"5420076", "address1"=>"大阪府", "address2"=>"大阪市中央区", "address3"=>"難波 5 -1 -60", "building_name"=>"イオ 15階", "phone_number"=>"0503187-577"}, "commit"=>"更新する", "controller"=>"searches", "action"=>"update", "id"=>"1"} permitted: false>
補足情報(FW/ツールのバージョンなど)
ruby '2.6.5'
Search.find(params[:id]) に変更前の値が入っていて変更できてません。
paramsには変更後の値が入っているのですが、どのように記述すればいいかわからないです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/26 09:11
2020/10/26 11:59
2020/10/27 03:18
2020/10/27 05:10
2020/10/27 06:44
2020/10/27 07:36
2020/10/27 07:47