【问题标题】:Rails route not finding corresponding actionRails路线没有找到相应的动作
【发布时间】:2013-02-26 21:34:56
【问题描述】:

在 app/views/participants/index.html.erb 内:

<%= form_tag bulk_add_participants_program_path do %>
  <%= wrap_control_group do %>
    <%= text_area_tag :bulk_add_participants, :size => "60x3" %>
    <% end %>
    <%= submit_tag "Import Participants and Users" %>
<% end %>

但请注意,控制器和路由与 Program 模型相关(出于良好的 UI 原因)。我认为这可能与问题有关。当我渲染该视图时,我收到以下错误消息:

No route matches {:action=>"bulk_add_participants", :controller=>"programs"}

这很奇怪,因为在 app/controllers/programs_controller.rb:

  def bulk_add_participants
    puts "yay!"  # because i am troubleshooting
  end

而我的 config/Routes.rb 是:

RepSurv::Application.routes.draw do

  root to: 'programs#index'

  devise_for :users, path_prefix: 'devise'
  resources :users

  resources :programs do
    resources :participants do
      resources :rounds do
        get 'survey' => 'rounds#present_survey'
        put 'survey' => 'rounds#store_survey'
      end
    end
    resources :questions
    resources :rounds
    member do
      get 'report' => 'reports#report'
      get 'bulk_add_participants'
    end
  end
end

【问题讨论】:

  • bulk_add_participants_program_url 也无法识别?运行rake routes 说明了这些路线?

标签: ruby-on-rails routes


【解决方案1】:

它没有找到路线,因为您将 programs 定义为复数资源:

resources :programs do

当您这样做并引用 member 路由(如您的 bulk_add_participants)时,在您的情况下它需要一个 :program_id 参数。 (尝试运行rake routes,您会看到类似/programs/:program_id/bulk_add_participants 的路径。)

所以你的form_tag 电话应该是这样的:

<%= form_tag bulk_add_participants_program_path(@program) do %>

【讨论】:

  • 谢谢!我以前被那个烫伤了,谢谢你额外的眼睛!
  • 不客气。 rake routes 是你在这种情况下的朋友。
  • 哦,但我确实多次搜索路由,我在路由中看到 /1/ 并尝试了它们等。但我的大脑没有将参数的需求连接到调用站点。坏脑袋。谢谢!
猜你喜欢
  • 2012-04-27
  • 2016-03-21
  • 2022-01-04
  • 2022-12-07
  • 1970-01-01
  • 2012-10-11
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多