2014-12-31 4 views
2

Я использую android spring и jackson для доступа к рельсам api. У меня есть модель под названием «заметки», которая принадлежит пользователю. Я могу успешно извлекать ноты, но когда я пытаюсь обновить один с «PUT» Я получаю эту ошибку:NoMethodError (undefined method `allow! 'For" ": String):

Started PUT "https://stackoverflow.com/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 

Является ли это проблемой с моим апи или json im отправка?

Кроме того, Heres моя весна "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 + "https://stackoverflow.com/users/"+userId+"/notes/"+note.getId(), HttpMethod.PUT, putEntity,Note.class, note.getId()); 
       Log.d("Response", new Gson().toJson(response)); 
       break; 

ответ

3

изменение:

def note_params 
    params.require(:note).permit.(:note, :user_id) 
    end 

к:

def note_params 
    params.require(:note).permit(:note, :user_id) 
    end 
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"} 

Здесь следует отметить, ключ, значение которого «тех га таня бушель изделие», и вы используете

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.

Смежные вопросы