首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向特定文章添加评论

向特定文章添加评论
EN

Stack Overflow用户
提问于 2015-05-06 20:26:08
回答 1查看 216关注 0票数 0

我是cakephp的新手,我刚刚完成了博客/文章教程,现在我正在尝试添加评论系统。

在我的注释表中,我有Id、article_Id、name、email、text和created。

我尝试根据文章ID添加评论,假设用户查看了一篇特定的文章,比如/view/1,他可以在页面底部为该文章添加评论。我该怎么做呢?我希望我说清楚了。

这是我对我的评论的添加函数:

代码语言:javascript
复制
    public function add()
  {
    $comment = $this->Comments->newEntity();
    if ($this->request->is('post')) {
    $comment = $this->Comments->patchEntity($comment, $this->request-   
    >data);
        if ($this->Comments->save($comment)) {
            $this->Flash->success('The comment has been saved.');
            return $this->redirect(['action' => 'index']);
        } else {
 $this->Flash->error('The comment could not be saved. Please, try  
  again.');
        }
    }
    $articles = $this->Comments->Articles->find('list', ['limit' => 200]);
    $this->set(compact('comment', 'articles'));
    $this->set('_serialize', ['comment']);
}

和我的add视图:

代码语言:javascript
复制
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
    <legend><?= __('Add Comment') ?></legend>
    <?php
        echo $this->Form->input('article_id', ['options' => $articles]);
        echo $this->Form->input('name');
        echo $this->Form->input('email');
        echo $this->Form->input('text');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
 <?= $this->Form->end() ?>

正如你所看到的,我正在从我的下拉列表中选择我的文章,但我不想这样做,我希望当用户查看该特定文章时,他们可以直接评论和查看。

EN

回答 1

Stack Overflow用户

发布于 2015-05-06 20:29:58

在该视图页面上添加评论表单-

代码语言:javascript
复制
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
    <legend><?= __('Add Comment') ?></legend>
    <?php
        echo $this->Form->hidden('article_id', ['value' => $articleId]);
        echo $this->Form->input('name');
        echo $this->Form->input('email');
        echo $this->Form->input('text');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

并为具有文章id的值的article_id设置隐藏字段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30076705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档