我想讓這個API工作,但已經到了這個錯誤,我不知道如何解決。我得到的title.I錯誤我使用郵遞員擴展從google chrome.And試圖做一個post請求param丟失或值爲空:公司
localhost:3000/companies
並且出現錯誤。它看起來像下面這樣
的ActionController :: ParameterMissing在
CompaniesController#create
參數是丟失或爲空值:公司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
。修改爲config/application
爲cors。
然後爲公司生成一個腳手架。
的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
應用程序/串行器/ 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
Yes.THANKS一噸。我做了公司[名稱],它的工作,非常感謝。 – IlearnedThat