従来のSQL Serverデータベースに接続する単純なRails APIを作成しています。私は自分のコンタクトコントローラのRESTアクションをテストしています。 FactoryGirlを使用してテストオブジェクトを作成すると、タイトルに記載されているエラーメッセージが表示されました。私のインデックスとショーの動作はうまくいきますが、作成アクションがこのエラーを投げています。私contacts_controllerの関連部分は次のようになります。"89718"の未定義のメソッド 'permit':文字列
describe "POST #create" do
context "when is successfully created" do
before(:each) do
@user = FactoryGirl.create :user
@contact = FactoryGirl.create :contact
post :create, { contact: @contact }
end
it "renders the json representation for the contact record just created" do
contact_response = json_response
expect(contact_response[:name]).to eq @contact_attributes[:name]
end
it { should respond_with 201 }
end
end
モデル::
class Contact < ActiveRecord::Base
belongs_to :user
validates :name, :address_1, :city, :zip_code_5, :country, :createddate, presence: true
end
(active_model_serializer宝石を使用して)シリアライザ
def create
contact = Contact.new(contact_params)
if contact.save
render json: contact, status: 201, location: [:api, contact]
else
render json: { errors: contact.errors }, status: 422
end
end
...
private
def contact_params
params.require(:contact).permit(:name, :address_1, :city, :zip_code_5, :country)
end
そしてここでは、関連するテストコードです:
class ContactSerializer < ActiveModel::Serializer
belongs_to :user
attributes :id, :name, :address_1, :city, :zip_code_5, :country
end
私が試した物事は、次のとおりです
- がpermiteから「zip_code_5」を削除シリアライザの「has_oneの」から「belongs_toの」(変更なし)
- を変更...、妙(ラインを必要とします私はまだ、おそらく理由シリアライザ?)
- シリアライザを削除(変更なし)
任意の考えでは、このプロパティに関するエラーメッセージが表示されましたか?私は必要な情報を提供してくれてうれしいです。
EDIT
それが作成するアクションに渡されます@contactの値:
#<Contact id: 89815, user_id: "d67b0d57-8f7f-4854-95b5-f07105741fa8", title: nil, firstname: nil, lastname: nil, name: "Alene Stark", company: nil, address_1: "72885 Bauch Island", address_2: nil, address_3: nil, city: "Joestad", state: nil, zip_code_5: "98117", zip_code_4: nil, country: "MF", status_id: 1, createddate: "2015-10-23 07:00:00", lastmodifieddate: "2012-11-29 08:00:00", errorreasonid: nil, computergenerated: true, sandbox: true, emailsubject: nil, jobtitle: nil, mergevar1: nil, mergevar2: nil, mergevar3: nil, mergevar4: nil, mergevar5: nil, mergevar6: nil, mergevar7: nil, mergevar8: nil, mergevar9: nil, mergevar10: nil, clientid: 1, isshared: true>
のparamsの値私も持っている
{"city"=>"Seattle", "state"=>"WA", "zip_code_5"=>"98117", "country"=>"US"}
:実行時に[連絡先]私のラップパラメータは、json形式に設定されています(該当する場合)。
コントローラの 'params [:contact]'は何らかの理由で文字列です。あなたが投稿するために渡している '@ contact'の値をチェックできますか?また、実行時に 'params [:contact]'の値をチェックし、それが何であるかを表示することもできます。 –