このAPIを動作させようとしていますが、解決方法がわからないこのエラーが発生しました。タイトルに誤りがあります。 Googleクロム。paramが存在しないか、値が空です。会社
localhost:3000/companies
で投稿リクエストを行いました。 CompaniesController#create
のparamに
ActionController :: ParameterMissingの下に次のようになりますどちらが欠けているか、値が空である:会社
def company_params (this line is highlighted) params.require(:company).permit(:name, :phone, :email, :website, :address, :customer_id, :additional_info) end end Request Parameters: {"name"=>"harun"}
私は、rails-api
を生成avtive_model_serializer
とrack-cors gem
を追加しました。 corsにはconfig/application
が変更されました。
そして、会社の足場を作りました。
Gemfile
gem 'active_model_serializers', '~> 0.10.0'
gem 'rack-cors'
設定/ application.rb
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
APP /シリアライザ/ company_serializer.rb
class CompanySerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :email, :website, :address, :customer_id, :additional_info
end
のコントローラ/ companies_controller.rb
class CompaniesController < ApplicationController
before_action :set_company, only: [:show, :update, :destroy]
def index
@companies = Company.all
render json: @companies
end
def show
render json: @company
end
def create
@company = Company.new(company_params)
if @company.save
render json: @company, status: :created, location: @company
else
render json: @company.errors, status: :unprocessable_entity
end
end
def update
@company = Company.find(params[:id])
if @company.update(company_params)
head :no_content
else
render json: @company.errors, status: :unprocessable_entity
end
end
def destroy
@company.destroy
head :no_content
end
private
def set_company
@company = Company.find(params[:id])
end
def company_params
params.require(:company).permit(:name, :phone, :email, :website, :address, :customer_id, :additional_info)
end
end
はTONをYes.THANKS。私は会社[名前]をして働いた。ありがとう。 – IlearnedThat