0

TL;DR: jQuery を使用したPOSTセットアップと、アクション (ポスト ハンドラー) のセットアップに問題があります。アクション/ビューをセットアップする方法とjQueryから投稿する方法の例を教えてください。


そのため、いくつか掘り下げましたが、まだ機能させることができず、不明な点が見つかりませんでした。だから、私は投稿部分を落としたと思いますが、投稿リクエストハンドラーをセットアップする方法がよくわかりません。より具体的には、メッセージ (成功/エラー/検証エラー) で適切な応答を返すことができるように、コントローラーのアクションとビューをどのようにセットアップすればよいかわかりません。ユーザー名については、電子メールを使用しており、ドキュメントで読んだことから、あなたが設定している限りidその後、記録を更新します。ただし、jQuery投稿の一部として送信されていないにもかかわらず、パスワードも更新されているため、非常に奇妙な問題が発生しています。もう 1 つのことは、メールを正常に更新できたにもかかわらず、現在表示しているページでメールが更新されていないことに気付きました。成功を確認した後、値を再設定する必要があると想定しています。誰か私に例を見せてくれますか?

ここに私が持っているものがあります:

アクション:

public function edit() {
    $this->autoRender = false; // I am not sure if I need this
    Configure::write('debug', 0 ); // I think this disables all the extra debug messages I get with jQuery
    $this->disableCache(); // No idea why I need this

    if($this->request->is('ajax')) {

        $id = $this->Auth->user('id');
        // Going to be adding other cases for name/password/etc...
        switch($this->params->data['post']) {
            case 'email':
                $result = $this->updateEmail($this, $id,  $this->params->data);
                break;

        }

    }

}

private function updateEmail($object, $id=null, $request=null) {
            // Do I need to re-log them back in after I change their email to create a new session?
    $object->AccountDetail->User->id = $id;
    if($object->AccountDetail->User->save($request)) {
        return $this->Session->setFlash(__('Your email has been updated!'));
    } else {
        return  $this->Session->setFlash(__($object->AccountDetail->User->validationErrors));
    }
}

jQuery の投稿:

 $('#email :button').click( function () {
        $.post('/account/edit', {post: 'email', email: $('#email').val() });
    });
4

1 に答える 1

1

これを試して。これは、行全体ではなく、フィールドのみを更新します。

saveField(<fieldname>, <data>, <validation>);   // structure of saveField() method

$object->AccountDetail->User->saveField('email', $request, false);

if($object->AccountDetail->User->saveField('email', $request, false)) {
    return $this->Session->setFlash(__('Your email has been updated!'));
} else {
    return  $this->Session->setFlash(__($object->AccountDetail->User->validationErrors));
}

updateEmail()関数をupdateField()次のように更新できます。

private function updateField($object, $field = null, $id=null, $request=null) {
            // Do I need to re-log them back in after I change their email to create a new session?
    $object->AccountDetail->User->id = $id;
    if($object->AccountDetail->User->saveField($field, $request, false)) {
        return $this->Session->setFlash(__("Your $field has been updated!"));
    } else {
        return  $this->Session->setFlash(__($object->AccountDetail->User->validationErrors));
    }
}

そして、次のように使用します。

$result = $this->updateField($this, 'email', $id,  $this->params->data);
于 2012-04-15T08:32:36.750 に答える