orchestral / platform

Orchestra Platform Application Skeleton
http://orchestraplatform.com/docs/latest
315 stars 33 forks source link

[Bug] Ajax call on a resource route gives error #32

Closed Fed03 closed 10 years ago

Fed03 commented 10 years ago

Hi, this is the setup

// locale.js

$.ajax({
    type: 'POST',
    url: '{{resources("chattranslation/ajax-release-translation")}}',
    data: { translationId: 'some-value', translation: 'somevalue' }
});
//ChatTranslationServiceProvider

public function boot()
{
    $path = realpath(__DIR__.'/../../');
    $this->package('chattranslation', 'chattranslation', $path);
    Event::listen('orchestra.started: admin', function () {
        $chat = Resources::make('chattranslation', array(
            'name'    => 'Messages Translation',
            'uses'    => 'restful:Turistas\ChatTranslation\Controller\TransController',
            'visible' => \Auth::check(),
        ));
    });

    Acl::make('chattranslation')->attach(App::memory());
}
//TransController

public function postAjaxReleaseTranslation()
{
    $translation = $this->translation->find(Input::get('translationId'));
    if (Input::get('translation')) {
        $translation->translated_body = Input::get('translation');
    } else {
        $translation->touch();
    }
    $translation->save();
}

now this configuration gives NotFoundHttpException but if I remove the data from the js like this

// locale.js

$.ajax({
    type: 'POST',
    url: '{{resources("chattranslation/ajax-release-translation")}}'
});

the NotFoundHttpException error vanishes instead it gives me the obvious error on $translation = $this->translation->find(Input::get('translationId')); this, however, tells us that it reached the right method.

The only way that works with sending data is to add a return statement inside the controller method like

//TransController

public function postAjaxReleaseTranslation()
{
    $translation = $this->translation->find(Input::get('translationId'));
    if (Input::get('translation')) {
        $translation->translated_body = Input::get('translation');
    } else {
        $translation->touch();
    }
    $translation->save();
    return true;
}

with this small addition everything works as intended but only because I don't care about data sent back to the client in this case.

Hope to have explained the problem throughly

crynobone commented 10 years ago

I did test it locally and can't replicate the issue, see https://github.com/crynobone/lightbulb/tree/test/ajax-resources

Fed03 commented 10 years ago

As soon as I can test it, I'll let you know

crynobone commented 10 years ago

@Fed03 okay, you can also try to make it fail and send a PR for the failing. So I can test the same code at my end.

crynobone commented 10 years ago

Fixed on orchestra/resources v2.0.5 & v2.1.1

Fed03 commented 10 years ago

ty @crynobone ^