简体   繁体   English

来自子控制器的response_to

[英]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: 我目前正在修改一个开源的Rails项目,在完成了许多教程和推荐后,我想在项目中进行一些导出,但我不断收到UnknownFormat错误,然后我意识到在所有项目控制器“ application_controller.rb”的父控制器中都有默认的response_to,如下所示:

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: unknownformat错误停止了,但随后我无法控制子控制器中的代码,似乎忽略了我的自定义格式方法的代码,直截了当地尝试寻找csv模板并将其呈现,子控制器具有以下功能:

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. 我在Rails方面不是很熟练,我的意思是这是我的第一个项目,我从修改现有项目开始,所以我不知道我应该如何告诉te applicaation_controller识别csv和xls格式,但前提是子控制器响应该格式。

I would look into why CanCan is going to access denied. 我将调查为什么CanCan会拒绝访问。 My initial guess is that you don't want to serve the CSV when a user is denied. 我最初的猜测是,当用户被拒绝时,您不想提供CSV。 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. 该方法是为了防止未经授权的访问,因此一旦建立访问权限,便可以将控制器设置为按所需的.csv路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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