我正在使用android spring和jackson访问rails api。我有一个名为“notes”的模型属于用户。我能够成功检索笔记但是当我尝试使用“PUT”更新笔记时,我收到此错误:
Started PUT "/users/1/notes/1" for 192.168.56.1 at 2014-12-30 17:18:18 -0800
Processing by Api::V1::NotesController#update as JSON
Parameters: {"note"=>"ta ga tane bu ware ", "id"=>"1", "noteText"=>"ta ga tane bu ware ", "user_id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'SmaxscWde3m7PKYEivC5' ORDER BY "users"."id" ASC LIMIT 1
Note Load (0.1ms) SELECT "notes".* FROM "notes" WHERE "notes"."id" = ? LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `permit' for "ta ga tane bu ware ":String):
app/controllers/api/v1/notes_controller.rb:42:in `note_params'
app/controllers/api/v1/notes_controller.rb:27:in `update'
这是我的notes_controller
class Api::V1::NotesController < ApplicationController
before_action :require_user
skip_before_filter :verify_authenticity_token
respond_to :json
def index
@note= Note.all
render json: current_user.notes
end
def show
render json: Note.find(params[:id])
end
def create
@note = Note.new note_params.merge(user_id: current_user.id)
if @note.save
render json: @note, status: 201, location: [:api, @note]
else
render json: {note:{ errors: @note.errors}}.to_json, status: 422
end
end
def update
@note = Note.find(params[:id])
if @note.update_attributes(note_params)
render json: @note, status: 200, location: [:api, @note]
else
render json: {note: {errors: @note.errors}}.to_json, status: 422
end
end
def destroy
@note = Note.find(params[:id])
@note.destroy
head 204
end
def note_params
params.require(:note).permit.(:note, :user_id)
end
end
这是我的api或json im发送的问题吗?
另外,继承我春天的“PUT”电话
case PUT:
headers.add("Authorization", "Token token=" +token);
headers.add("Content-Type", "application/json");
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
HttpEntity<Note>putEntity = new HttpEntity<Note>(note,headers);
response = restTemplate.exchange(config.WEB_SERVICE_URL + "/users/"+userId+"/notes/"+note.getId(), HttpMethod.PUT, putEntity,Note.class, note.getId());
Log.d("Response", new Gson().toJson(response));
break;
答案 0 :(得分:3)
改变:
def note_params
params.require(:note).permit.(:note, :user_id)
end
到:
def note_params
params.require(:note).permit(:note, :user_id)
end
答案 1 :(得分:3)
你需要做
def note_params
params.permit(:note, :user_id)
end
您获得的参数是:
Parameters: {"note"=>"ta ga tane bu ware ", "id"=>"1", "noteText"=>"ta ga tane bu ware ", "user_id"=>"1"}
这里的注释是关键,其价值是&#34; ta ga tane bu ware&#34;你正在使用
params.require(:note).permit.(:note, :user_id)
仅当您的参数哈希如下
时才会起作用Parameters: {"note" => {"note"=>"ta ga tane bu ware ", "id"=>"1", "noteText"=>"ta ga tane bu ware ", "user_id"=>"1"}}
有关详细信息,请查看this。