Ajax Star Rating Doesn't Save Into Database

Hello all

Probably my action for ajax star rating fails to save data into db.

I’ve got ajax star rating in my view




$this->widget('CStarRating', array(

            'id' => $data->id,

            'attribute' => 'rating',

            'name' => 'rating[' . $data->id . ']',

            'value' => $data->rating,

            'callback' => '

             function(){

                $.ajax({

                    type: "POST",

                    url: "' . Yii::app()->createUrl('line/starRatingAjax') . '",

                    data: { id: "' . $data->id . '" , rate: "$(this).val()"},

                    success: function(){

                                $("#result' .$data->id. '").fadeIn().text("Thanks for vote !");

                        }})}'

        ));



And an action in my controller :




public function actionStarRatingAjax() {

        if (Yii::app()->request->isAjaxRequest) {

            $model = $this->loadModel($_POST['id']);

            $model->rating = $_POST['rate'];

            $model->save();

            } 

        }



It actually views success message but that’s all.

Thank you in advance.

Hi,

This:




data: { id: "' . $data->id . '" , rate: "$(this).val()"},



Should be:




data: { id: "' . $data->id . '" , rate: $(this).val()},



thanks for pointing out but still doesn’t work :(

Just to be sure I created a table with “id” and “rating” fields and a corresponding model. Also added a controller and a view with your code. Ratings are saved without errors. So you’ve made a mistake somewhere else then in the provided code. Check your model rules() method. “rating” field should be there.