简体   繁体   English

Ruby On Rails:将params传递给link_to以调用创建路径

[英]Ruby On Rails: pass params to link_to to call the create path

I am writing a report module that allows users to select the dates of the report and export the corresponding report to excel. 我正在编写一个报告模块,允许用户选择报告的日期并将相应的报告导出到Excel。 So, I use a active model to store the dates. 因此,我使用活动模型来存储日期。 In the view, I have a link for users to download reports. 在视图中,我有一个供用户下载报告的链接。 I want it goes to the create action in the controller so that I can get the report with correct date. 我希望它转到控制器中的创建操作,以便我可以获得具有正确日期的报告。

<%= render partial: 'reports/select_dates_form' %>
        <p>
            Download:
            <%= link_to "Excel", reports_A_reports_path(:report, :end_date => @report.end_date, :start_date => @report.start_date), :method => :post, format: "xls" %>
        </p>

In the controller, I defined a create_param method to define the params. 在控制器中,我定义了一个create_param方法来定义params。

def create_params
    params.require(:reports_A_report).permit(:end_date, :start_date)
end

However, it gives error "param is missing or the value is empty: reports_A_report". 但是,它会出现错误“param丢失或值为空:reports_A_report”。 I try to various way to pass the params but still cannot success. 我尝试以各种方式通过params但仍然无法成功。

In the standard REST scheme the index action and the create action both have the same url (/reports) and only differ in that index is accessed using GET and create is accessed using POST. 在标准REST方案中,索引操作和创建操作都具有相同的URL(/ reports),并且仅在使用GET访问索引并且使用POST访问create时不同。 So link_to :action => :create will simply generate a link to /reports which will cause the browser to perform a GET request for /reports when clicked and thus invoke the index action. 所以link_to :action => :create将生成一个指向/reports的链接,这将导致浏览器在单击时执行/reports的GET请求,从而调用索引操作。

To invoke the create action use link_to {:action => :create}, :method => :post , telling link_to explicitly that you want a post request, or use a form with a submit button rather than a link. 要调用create动作,请使用link_to {:action => :create}, :method => :post link_to link_to {:action => :create}, :method => :post ,明确告诉link_to您想要发布请求,或使用带有提交按钮而非链接的表单。

Try 尝试

<%= link_to "Text_to_dispaly", :controller => "controller_name", :action => "action_name, :method => :post" %>

Also, method is for choosing the HTTP method (GET, POST, ...). 此外, method是选择HTTP方法(GET,POST,...)。 It's not method as in routine. 这不是常规的方法。

Be sure to check out Rails Routing from the Outside In and The Lowdown on Routes in Rails 3 , they're both awesome resources. 一定要查看Rails 3中 的外部输入路由下的Rails路由 ,它们都是很棒的资源。

I hope my answer help you.. 我希望我的回答能帮到你..

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

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