简体   繁体   中英

respond_to from child controller

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM