2012-10-20 101 views
0

你能幫我重構和DRY嗎?我沒有想法。謝謝。Rails,重構控制器

if request.xhr? 
    render :json => { 
    :status => true, 
    :location => root_url + "/projects", 
    :message => I18n.t("project.destroy") 
    } 
else 
    flash[:notice] = I18n.t("project.destroy") 
    redirect_to :action => :index 
end 

回答

1

嗯,你可以在application_controller.rb覆蓋redirect_to的這樣

def redirect_to(options={}, response_status={}) 
    if request.xhr? 
    render :json => { :status => true, :location => options, :message => flash[:notice] } 
    else 
    super 
    end 
end 

,然後繼續使用

flash[:notice] = I18n.t('project.destroy') 
redirect_to projects_path 

在你的控制器中奧勒。

1

沒有什麼可以做,但

message = I18n.t('project.destroy') 

return render :json => { 
     :status => true, 
     :location => "#{root_url}/projects", 
     :message => message 
     } if request.xhr? 

flash[:notice] = message 
redirect_to :action => :index