Ich versuche, Daten von JSON-Anforderung zu speichern, die ich auf meinem Server sende. Ich habe bereits Ausnahmen zu CSRF Verteidiger hinzugefügt und es Anforderungen besteht. ActiveRecordsspeichern Daten nicht von ihm, aber es funktioniert für HTML-Anforderungen.Rails 4: ActiveRecords kann keine Daten von JSON speichern
Ich benutze curl JSON zu erzeugen, fordert
curl -v -X POST -d '{"claim": {"lastname":"Jhon Smith","phone":"+1(51)555555","latitude":"10.4","longitude":"12.7","theme":"Test message","text":"Text of test message"}}' -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:3000/claims/new
Es gibt Nachrichten-Code 200.
> POST /claims/new HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.43.0
> Content-Type:application/json
> Accept:application/json
> Content-Length: 167
>
* upload completely sent off: 167 out of 167 bytes
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Cache-Control: no-cache
Server antwortet, dass es Daten gefangen, aber etwas nicht tun.
Started POST "/claims/new" for 127.0.0.1 at 2016-04-16 01:35:24 +0300
Processing by ClaimsController#new as JSON
Parameters: {"claim"=>{"lastname"=>"Jhon Smith", "phone"=>"+1(51)555555", "latitude"=>"10.4", "longitude"=>"12.7", "theme"=>"Test message", "text"=>"Text of test message"}}
Rendered claims/new.json.erb (0.0ms)
Completed 200 OK in 2ms (Views: 1.2ms | ActiveRecord: 0.0ms)
Es gibt Code meines Controller, die JSON zu speichern versuchen:
def create
@claim = Claim.new(claim_params)
respond_to do |format|
if @claim.save
format.html do
redirect_to acceptedclaim_path
flash[:success] = "Ваша заява прийнята! Дякуємо за допомогу!"
end
format.json do
render json: @claim, status: :created, location: @claim
end
else
format.html do
redirect_to new_claim_path
flash[:danger] = flash_errors(@claim)
end
format.json do
render json: @claim.errors, status: :unprocessable_entity
end
end
end
end
def claim_params
params.require(:claim).permit(:lastname, :phone, :latitude, :longitude, :theme, :text)
end
ich Zugriff auf POST über nächsten config in Router
post 'claims/new' => 'claims#new'
ich dankbar sein werde für irgendeine Hilfe!
Danke! Es klappt! – Ic2h