2017-06-07 3 views
0

respond_toにrespond_to:UIを使用してAPIのJSONのアップロード、または手動アップロードに対応できるAPIについてレールは、コントローラの救助ブロック内で動作していないコントローラの救助ブロック

def create 
    respond_to do | format | 
    format.json { render json: SomeManager.new(some_params).json } 
    format.html do 
     SomeManager.new(some_params) 
     render :new, notice: 'it worked' 
    end 
    end 
rescue => e 
    respond_to do | format | 
    format.json { render json: {error: 'did not work because reasons'}.to_json, status: :forbidden } 
    format.html { render :new, alert: 'did not work because reasons' } 
    end 
end 

^コントローラー。読みやすくするために

+0

ああ...これが唯一の理由は別の場所でエラーの働いていなかった私の特定のケースでは、ファイルのアップロードがビューになるために必要な:そうは次のようにあなたのroutes.rbをオプションで指定された場合、これはどうなります'= form_tag({action::create、format: 'html'}、multipart:true)do'のように、書式がhtmlであることを追加する必要がありました – xxjjnn

答えて

1

beginを使用..代わりに、このようなrescue:私はそれがうまくならないと思います

def create 
    respond_to do | format | 
    begin 
     format.json { render json: SomeManager.new(some_params).json } 
     format.html do 
     SomeManager.new(some_params) 
     render :new, notice: 'it worked' 
     end 
    rescue => e 
     format.json { render json: {error: 'did not work because reasons'}.to_json, status: :forbidden } 
     format.html { render :new, alert: 'did not work because reasons' } 
    end 
    end 
end 

0

レスキューブロックは責任を負いません。

resources :widgets, defaults: {format: :the_spanish_inquisition} 
関連する問題