Cakephp使用CakeDC插件获得五星评级

时间:2016-06-02 22:17:51

标签: cakephp cakephp-2.0 a-star rating cakedc

我是初学者。我使用CakeDC评级在我的网站上进行星级评分。我设法让它工作,但它显示无线电选择。

它告诉我错误:

  

未定义的变量:item

并按下"评分"后,网址将更改为avis / post / view / redirect:1

probleme

我下载插件并在C:\wamp\www\avis\app\Plugin中解压缩 在app / Config / bootstrap.php中写CakePlugin::load('Ratings')后,我的数据库是

CREATE TABLE IF NOT EXISTS `posts` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;


  CREATE TABLE IF NOT EXISTS `ratings` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `user_id` char(36) NOT NULL DEFAULT '',
  `foreign_key` char(36) NOT NULL DEFAULT '',
  `model` varchar(100) NOT NULL DEFAULT '',
  `value` float NOT NULL,
   `created` datetime DEFAULT NULL,
   `modified` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
   KEY `rating` (`user_id`,`model`,`foreign_key`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
在Post Model中

      public $hasMany = array(
       'Rating' => array(
        'className' => 'Rating',
        'foreignKey' => 'foreign_key',
        'dependent' => false,

        )
      );
PostController中的

  public $components = array('Paginator', 'Flash',    'Session','Ratings.Ratings');
           public $actsAs = array('Ratings.Ratable');

          public function index() {
    $this->Post->recursive = 0;
    $this->set('posts', $this->Paginator->paginate());
}

public function view($id = null) {
    if (!$this->Post->exists($id)) {
        throw new NotFoundException(__('Invalid post'));
    }
    $options = array('conditions' => array('Post.' . $this->Post->primaryKey => $id));
    $this->set('post', $this->Post->find('first', $options));
}

在视图中

    <h2><?php echo __('Post'); ?></h2>
<dl>
    <dt><?php echo __('Id'); ?></dt>
    <dd>
        <?php echo h($post['Post']['id']); ?>
        &nbsp;
    </dd>
    <dt><?php echo __('Title'); ?></dt>
    <dd>
        <?php echo h($post['Post']['title']); ?>
        &nbsp;
    </dd>
    <?php 
echo $this->Rating->display(array(
    'item' => $post['Post']['id'],
'type' => 'radio',
'stars' => 5,
'value' => $item['rating'],
'createForm' => array(
    'url' => array(
        $this->passedArgs, 'rate' => $item['id'],
        'redirect' => true
    )
)
));
  ?>
   </dl>
 </div>  

<script>
            $('#ratingform').stars({
        split:2,
          cancelShow:false,
        callback: function(ui, type, value) {
    ui.$form.submit();
   }
   });
  </script>

1 个答案:

答案 0 :(得分:0)

  1. 首先下载Jquery Raty Js插件来自:https://github.com/wbotelhos/raty
  2. 在您的视图中加入js,css

    <div class="jrating" data-score="5"></div>
    <script> $('.jrating').raty({ score: function() {return $(this).attr('data-score');} });
    </script>

    当您发布表单

    时,这将创建一个隐藏文本字段

    在控制器上: $ rating = $ this-&gt; data ['score']; 现在,您可以在表格中保存评级