2016-04-11 6 views
2

ItemsContainerという名前の親の中に存在するItemsというコンポーネントがあります。 Itemsのボタンがクリックされると、そのItemを削除するためにAjax関数が呼び出されます。Ajaxリクエストを削除するReactJSとRuby on Rails

ただし、私は500のエラーメッセージを受け取りました。理由は不明です。新しい項目として働いているの作成

項目コンポーネント

class Item extends React.Component{ 

constructor(props) { 
    super() 

    this.state = { 
     name: '', 
     price: 0, 
     code: '', 
     id: '' 
    } 
} 

componentWillMount() { 
    this.setState({ 
     name: this.props.data.name, 
     price: this.props.data.price, 
     code: this.props.data.code, 
     id: this.props.data.id 
    }) 
} 

deleteItem() { 
    let finalUrl = '/items/' + this.state.id; 
    $.ajax({ 
     type: "DELETE", 
     url: finalUrl, /* THIS URL IS CALLING CORRECTLY ie. /items/8 */ 
     dataType: "json", 
     success: function(response) { 
      console.log("successfully deleted"); 
     }, 
     error: function() { 
      console.log("error"); 
     } 
    }) 
} 


render(){ 
    let itemName = this.props.data.name 
    let itemCode = this.props.data.code 
    let itemQuantity = 1 
    let itemPrice = (this.props.data.price * itemQuantity).toFixed(2) 
    const itemId = this.props.data.id 

    return(
     <tr> 
      <td>{itemName}</td> 
      <td>{itemCode}</td> 
      <td>{itemQuantity}</td> 
      <td><button className="btn btn-warning" onClick={this.deleteItem.bind(this)}>Remove</button></td> 
      <td>£{itemPrice}</td> 
     </tr> 
    ) 
} 

}

Railsのアイテムコントローラ

class ItemsController < ApplicationController 

def create 
    @item = Item.new(item_params) 
    if @item.save 
     render partial: 'items/item', locals: {item: @item} 
    else 
     render json: @item.errors.to_json 
    end 
end 

def destroy 
    if @item.destroy 
     render partial: 'items/item', locals: {item: @item} 
    else 
     render json: @item.errors.to_json 
    end 
end 

private 

def item_params 
    params.require(:item).permit(
     :name, 
     :price, 
     :code, 
     :id 
    ) 
end 

エンド

予想されていますが、私は私の削除アクションで500のエラーを受け取っている理由を理解できません。どんな助けでも大歓迎です。

+1

レール部分の破壊方法を確認してください。私は欠けていると思う@項目?もし私が正しければ? – Amit

+1

ああ、ありがとうございました。私は@item = Item.find(params [:id])を使用しました。今はうまく動作しています:) –

+0

経験がないのでレールはありません:) – Amit

答えて

2

レールコントローラの破壊方法を確認してください。 @itemの定義はありません。したがって、内部サーバーのエラー500: