2016-07-10 7 views
0

、私のエンバーのフロントエンドは、私にこのようなデータを送信している:准レールAPIと燃えさし

ここ
{ 
    "data":{ 
     "attributes":{ 
     "title":"asd", 
     "description":"asd" 
     }, 
     "relationships":{ 
     "users":{ 
      "data":[ 
       { 
        "type":"users", 
        "id":"1" 
       } 
      ] 
     } 
     }, 
     "type":"cards" 
    } 
} 

、私は新しいcardが作成されたときにcarduserの間で関連付けたいです。

アプリ/コントローラ/ cards_controller.rb

def create 
    @card = Card.new(card_params) 

    if @card.save 
    @card.users << User.find(params[:data][:relationships][:users][:data]) if params[:data][:relationships][:users][:data] 

    render json: @card 
    else 
    render json: @card, :status => 422 
    end 
end 

private 

def card_params 
    params.require(:data).require(:attributes).permit(:title, :description) 
end 

Railsのエンドでエラーがこのようなものです:

DEPRECATION WARNING: Method to_a is deprecated and will be removed in Rails 5.1, as `ActionController::Parameters` no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.0/classes/ActionController/Parameters.html (called from create at /Applications/AMPPS/www/development/ideast/hub/hub-server/app/controllers/cards_controller.rb:16) 
Completed 500 Internal Server Error in 24ms (ActiveRecord: 3.4ms) 

NoMethodError (undefined method `join' for #<ActionController::Parameters:0x007f9b5549e380> 
Did you mean? JSON): 

レポリンク:私はあなたができるはずであると信じてhttps://github.com/ghoshnirmalya/hub-server

答えて

1

Emberアダプタをカスタマイズして正しいデータ形式を送信します。

しかし、ここで私はおそらく、現在の問題を解決して行くだろう方法です:

** user_params方法はとてもきれいではない、私はおそらくそれを処理するクリーンな方法を見つけたいと思います。

def create 
    @card = Card.new(card_params) 

    if @card.save 
    @card.users << User.find(user_params) 

    render json: @card 
    else 
    render json: @card, :status => 422 
    end 
end 

private 

def user_params 
    params.require(:data).require(:relationships).require(:users).permit(data: [:id]).require(:data).map(&:values) 
end 

def card_params 
    params.require(:data).require(:attributes).permit(:title, :description) 
end 
関連する問題