简体   繁体   中英

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. 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.

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". I try to various way to pass the params but still cannot success.

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. 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.

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.

Try

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

Also, method is for choosing the HTTP method (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.

I hope my answer help you..

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