respond_to from child controller

68 views Asked by At

Im currently modding an open source rails project and i want to do some exports in the project after following many tutorials and recomendations i keep getting the UnknownFormat error, then i realize that in parent controller for all the project controllers "application_controller.rb" there was a default respond_to, like this:

rescue_from CanCan::AccessDenied do |exception|
    respond_to do |format|
        format.html { redirect_to main_app.root_url, alert: exception.message }
        format.json { render json: {error: exception.message}, status: :forbidden }
    end
end

then i decided to turn it into this

rescue_from CanCan::AccessDenied do |exception|
    respond_to do |format|
        format.html { redirect_to main_app.root_url, alert: exception.message }
        format.json { render json: {error: exception.message}, status: :forbidden }
       format.csv
       format.xls
    end
end

the unknownformat error stopped but then i have no control of what to do in the child controller the code it seems to ignore my custom format method and goes straigth to try to look for a csv template and render it, the child controller have this:

respond_to do |format|
    format.html
    format.csv { send_data @users.to_csv }
    format.xls
end

im not very skilled in rails i mean this is my first project and i start by modding an existing one so i have no clue of what should i do to tell te applicaation_controller to recognize the csv and xls format but only if the child controller respond to that format.

1

There are 1 answers

0
Dalemonaco On

I would look into why CanCan is going to access denied. My initial guess is that you don't want to serve the CSV when a user is denied. That method is to prevent unauthorized access, so once you have access established you can then setup the controller to hit the .csv route you would like.