2016-04-11 4 views
2

ItemsContainer adlı bir ebeveynin içinde yaşayan Öğeler adlı bir bileşenim var. Öğelerdeki bir düğme tıklandığında, o Öğeyi silmek için Ajax işlevi çağrılır.Ajax Silme isteği ReactJS ve Ruby on Rails

Şu anda 500 hata iletisi alıyorum ve neden emin değilim. Yeni Öğe olarak çalışmaktadır oluşturma

Öğe Bileşen

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

}

Raylar Öğeler Kontrolör

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 

bekleniyordu ancak neden silme eylemim için 500 halimin neden alındığını anlayamıyorum. Herhangi bir yardım çok takdir edilecektir.

+1

Lütfen ray bölümünde imha yönteminizi kontrol edin. Ben onun @item eksik olduğunu düşünüyorum? eğer haklıysam – Amit

+1

Ah evet, teşekkürler bu çalıştı. Ben @item = Item.find (params [: id]) kullandım ve şimdi iyi çalışıyor :) –

+0

Bu konuda emin değilim çünkü hiç tecrübem yok raylar :) – Amit

cevap

2

Lütfen ray kontrol cihazında imha yönteminizi kontrol edin. @item hence 500 internal server hatası için bir tanım yok :)