1

我已经为发布和评论制作了控制器和模型。

我已经创建了我的帖子索引来调用我的添加评论视图。它工作正常,但添加评论视图打开另一个页面。

然后,我将添加评论视图包含在一个元素中,以便直接调用我的帖子页面,但是当我单击我的评论添加视图的提交按钮时,它不会保存任何内容。

当我查看提交按钮显示的链接时,它指向我的帖子索引视图。

如何使它起作用?

这是我的评论元素的代码

div class="comments form">
<?php echo $this->Form->create('Comment'); ?>
    <fieldset>
        <legend><?php echo __('Add Comment'); ?></legend>
    <?php


        $this->request->data['Comment']['user_id'] = $current_user['id'];
        $this->request->data['Comment']['post_id'] = $post_id;

        echo $this->Form->input('post_id', array('type' => 'hidden'));
        echo $this->Form->input('user_id', array('type' => 'hidden'));

        echo $this->Form->input('content');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

这是调用该元素的索引帖子视图的代码的一部分

<div class="element">
        <?php echo $this->element('add_comment', array('post_id' => $post['Post']['id'])); ?>
        </div>

我必须补充一点,这个 div 类元素位于可点击的 div 中。但是对于我在视图中的其他按钮,单击效果很好。


4

1 回答 1

1

更改表单的 url 以将其定向到 Comments 控制器中的添加操作,如下所示

echo $this->Form->create('Comment', array(
    'url' => array('controller' => 'comments', 'action' => 'add')
));

默认情况下,如果你不传递 url` 参数,表单会指向当前被渲染的动作(在本例中是 post 控制器),所以你需要指定你希望它发布到哪里。检查文档以了解该选项。

于 2013-07-24T20:34:10.530 回答