発生している問題・エラーメッセージ
updateアクションの値が変更されなく、エラーは出ておりません。
createは正常に動きます。
ソースコード
views/searches/edit.html.erb
<%= form_with(model: @search, local: true) do |f| %>
<%= f.label :postal_code,"郵便番号" %>
<%= f.text_field :postal_code, class: "search_form" %>
<%= f.label :address1,"都道府県" %>
<%= f.text_field :address1, class: "search_form" %>
<%= f.label :address2,"市区町村" %>
<%= f.text_field :address2, class: "search_form" %>
<%= f.label :address3,"町域" %>
<%= f.text_field :address3, class: "search_form" %>
<%= f.label :building_name,"建物名" %>
<%= f.text_field :building_name, class: "search_form" %>
<%= f.label :phone_number,"電話番号" %>
<%= f.text_field :phone_number, class: "search_form" %>
<br>
<%= f.submit class: "button is-warning"%>
<% end %>
controllers/searches_controller.rb
class SearchesController < ApplicationController
def show
@search = Search.find(params[:id])
end
def index
@search = Search.all
end
def edit
@search = Search.find(params[:id])
end
def update
@search = Search.find(params[:id])
@search.update(search_params)
redirect_to root_path
end
def create
@search = Search.new(search_params)
@search.user_id = current_user.id
@search.save!
redirect_to root_path
end
def search
if postal_code = params[:postal_code]
params = URI.encode_www_form({zipcode: postal_code})
uri = URI.parse("http://zipcloud.ibsnet.co.jp/api/search?#{params}")
response = Net::HTTP.get_response(uri)
result = JSON.parse(response.body)
if result["results"]
@zipcode = result["results"][0]["zipcode"]
@address1 = result["results"][0]["address1"]
@address2 = result["results"][0]["address2"]
@address3 = result["results"][0]["address3"]
end
end
end
end
private
def search_params
params.permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code)
end
config/routes.rb
Rails.application.routes.draw do
get 'searches',to: "searches#search"
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
devise_for :users, controllers: {
omniauth_callbacks: 'users/omniauth_callbacks',
registrations: 'users/registrations'
}
get 'homes/index'
resources :homes, only: [:index]
root to: 'home#index'
resources :users
resources :attendances
resources :daytimes
resources :restaurants, only: [:index, :show]
resources :searches do
collection do
get 'search'
end
end
namespace :admin do
resources :restaurants, only: [:index, :new, :create, :show, :edit, :destroy]
end
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>
params.require(:search).permit(:address1, :address2, :address3, :building_name,:phone_number, :postal_code)
とコントローラーに.require(:search)を記述したら、updateはされずcreateで、エラーが出ます。
ctionController::ParameterMissing in SearchesController#create
param is missing or the value is empty: search
Request
Parameters:
{"_method"=>"search",
"authenticity_token"=>"DiSrEfma6Y5g7xKmfPCILZwDQEz49fHKc76kmo+VKgg+9gxGwH6yWbfNR2im17YrNuSQ8eB+nWqGyIX/jM6OIw==",
"postal_code"=>"5420076",
"address1"=>"大阪府",
"address2"=>"大阪市中央区",
"address3"=>"難波",
"building_name"=>"なんば",
"phone_number"=>"090-1234-5678",
"commit"=>"保存する"}