带有成员获取路由的控制器操作的 form_tag 问题

发布于 2024-10-20 10:50:37 字数 1605 浏览 3 评论 0原文

我正在制作一个 form_tag 面板,其中包含特定于控制器操作的信息(复选框)。此操作在“routes.rb”中设置如下:

resources :students do
  collection do
    get :send_student_report_pdf
  end   
end

当我从 link_to 调用该操作时,此设置工作完美:

<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>

但是,当我在 form_tag 中使用它时,它一直给我这个错误:

Routing Error
No route matches "/students/send_student_report_pdf"

我的 form_tag 代码在这里:

<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
  <%= label_tag "Include columns" %> <br>
  <%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>  
  <%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>  
<% end %>

我尝试给它提供 url、路径,例如:

<%= form_tag send_student_report_pdf_students_path, :method => 'get' do %>

但它一直给我相同的路线错误(就好像该操作没有在routes.rb中根本不存在,即使它使用link_to而不是form_tag Submit完美地工作

这里是控制器中操作的代码,它基本上发送回来 内容

def send_student_report_pdf
  @students = search_sort_and_paginate
  puts "params[:first_name] = ", params[:first_namea]
  send_data(generate_pdf_report(@students), :filename => "report.pdf", :type => 'application/pdf') 
end

如果您发现我缺少某些

,请帮助我,非常感谢,

问候,

I'm making a form_tag panel that contains information (checkboxes) specific to a controller action. This action is set up in "routes.rb" as follows:

resources :students do
  collection do
    get :send_student_report_pdf
  end   
end

This setup works perfectly when I call the action from a link_to:

<%= link_to "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>

However when I used it in a form_tag, it keeps giving me this error:

Routing Error
No route matches "/students/send_student_report_pdf"

The form_tag code I have is here:

<%= form_tag :controller => 'students', :action => 'send_student_report_pdf', :method => 'get' do %>
  <%= label_tag "Include columns" %> <br>
  <%= check_box_tag "first_name", params[:first_name], checked = true %> <%= label_tag "First Name" %><br>  
  <%= submit_tag "Download PDF Report", :action => 'send_student_report_pdf', :controller => 'students'%>  
<% end %>

I have tried giving it the url, path like:

<%= form_tag send_student_report_pdf_students_path, :method => 'get' do %>

But it has been consistently giving me the same Route error (as if the action doesn't exist at all in routes.rb, even though it works perfectly using link_to instead of form_tag submit

Here is the code for the action in the controller, it basically sends back a file.

def send_student_report_pdf
  @students = search_sort_and_paginate
  puts "params[:first_name] = ", params[:first_namea]
  send_data(generate_pdf_report(@students), :filename => "report.pdf", :type => 'application/pdf') 
end

If you see that I'm missing something here, please help me.

Thank you very much,

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-10-27 10:50:37

:method =>; form_for 中的 'get' 部分位于 url_for_options 哈希中,而不是 options 哈希中,因此 Rails 会将其作为 cgi 参数放入 url 中。尝试将其更改为:

form_tag url_for(:controller => 'students', :action => 'send_student_report_pdf'), :method => 'get' do ...

您无法使用命名路由的原因是因为您没有在路由中命名它。如果您在路由中命名并在 form_tag 中使用命名路由,则不需要使用 url_for...

resources :students do
  collection do
    get :send_student_report_pdf, :as => :send_student_report_pdf
  end   
end

您可以通过运行 rake paths 来检查您的路由是否符合您的预期

The :method => 'get' part in your form_for is in the url_for_options hash, not the options hash, so Rails will be putting it onto the url as cgi params instead. Try changing it to this:

form_tag url_for(:controller => 'students', :action => 'send_student_report_pdf'), :method => 'get' do ...

The reason you can't use the named route is because you didn't name it in your routes. If you name it in your routes and use the named route in your form_tag, you won't need to use url_for...

resources :students do
  collection do
    get :send_student_report_pdf, :as => :send_student_report_pdf
  end   
end

You can check whether your routes are as you expect by running rake routes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文