Update action does not update data

Asked 2 years ago, Updated 2 years ago, 387 views

The value of the update action has not been changed, and there are no errors.
Create works fine.

views/searches/edit.html.erb

<%=form_with(model:@search,local:true)do|f|%>
      <%=f.label:postal_code, "Zip Code" %>
      <%=f.text_field:postal_code, class:"search_form"%>
      <%=f.label:address1, "Province" %>
      <%=f.text_field:address1,class:"search_form"%>
      <%=f.label:address2, "City"%>
      <%=f.text_field:address2,class:"search_form"%>
      <%=f.label:address3, "town area"%>
      <%=f.text_field:address3,class:"search_form"%>
      <%=f.label:building_name, "building name" %>
      <%=f.text_field:building_name, class:"search_form"%>
      <%=f.label:phone_number, "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

  default
    @search=Search.find (params[:id])
  end

  default 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
  param.permit(:address1,:address2,:address3,:building_name,:phone_number,:postal_code)
 end

config/routes.rb

Rails.application.routes.drawdo
  get 'search', to: "search #search"
  mount RailsAdmin::Engine=>'/admin', as: 'rails_admin'
  device_for:users,controllers:{
   omniauth_callbacks: 'users/mniauth_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

Tried

I put binding.pry on the update action

[2]pry(#<SearchController>)>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: "Osaka prefecture",
address2: "Osaka City Chuo Ward",
address3: "Namba 5-1-60",
building_name: "Namba Skyo 15th floor",
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"=> "Update", "controller"=> "searches", "action"=> "update", "id"=>"1"}permitted: false>

params.require(:search).permit(:address1,:address2,:address3,:building_name,:phone_number,:postal_code)

If you write .require(:search) on the and the controller, you will see an error in create instead of updating.

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" = > "Osaka Prefecture",
"address2" = > "Osaka Chuo Ward",
"address3" = > "Namba",
"building_name" = > "number",
"phone_number" = > "090-1234-5678",
"commit" = > "Save"}

ruby-on-rails ruby database

2022-09-30 21:54

1 Answers

I set the strong parameters for the update and was able to update them.

def update_params
  param.require(:search).permit(:address1,:address2,:address3,:building_name,:phone_number,:postal_code)
end


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.