簡體   English   中英

NoMethodError(“”:String的未定義方法“ permit!”):

[英]NoMethodError (undefined method `permit!' for “ ”:String):

我正在使用android spring和jackson訪問rails api。 我有一個屬於用戶的名為“筆記”的模型。 我能夠成功檢索筆記,但是當我嘗試使用“ 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;

更改:

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

至:

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

你需要做

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"}

這里的注釋是鍵,其值為“ ta ga tane bu ware”,您正在使用

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"}}

有關更多信息,請檢查

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM