【问题标题】:NoMethodError (undefined method `permit!' for " ":String):NoMethodError(未定义的方法 `permit!' for " ":String):
【发布时间】:2014-12-31 01:22:31
【问题描述】:

我正在使用 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 的问题吗?

另外,这是我的春季“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;

【问题讨论】:

    标签: android ruby-on-rails json spring rest


    【解决方案1】:

    改变:

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

    到:

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

    【讨论】:

      【解决方案2】:

      你需要做的

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

      更多信息请查看this

      【讨论】:

        猜你喜欢
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多