ActionController :: RoutingError - 没有路由匹配

时间:2014-02-25 10:49:49

标签: ruby-on-rails ruby routes

我有这条路线:

rake routes:

channel_import GET    /channels/:channel_id/import(.:format)    channels#import

但是当我尝试在link_to中使用它时:

<%= link_to "Import", channel_import_path(:hit_id => index), :method=>:get %>

我收到此错误:

ActionController::RoutingError (No route matches {:action=>"import", :controller=>"channels", :hit_id=>0}):

这是我在routes.rb中添加的路径:

resources :channels do
    get 'import' => 'channels#import'
end

这是我的控制者:

class ChannelsController < ApplicationController
  def import
    puts "import action"
    head :ok
  end
  ...
end

3 个答案:

答案 0 :(得分:4)

您还需要在链接参数中传递channel_id,例如

<%= link_to "Import", channel_import_path(:hit_id => index, :channel_id => @channel.id), :method=>:get %>

对于上面的输出路径,类似于/channels/21/import?hit_id=12

答案 1 :(得分:1)

您在链接中设置了:hit_id参数,而您的路线需要:channel_id。这应该有效:

<%= link_to "Import", channel_import_path(channel_id: 'your_channel_id') %>

答案 2 :(得分:1)

不知道你使用hit_id的原因,应该是channel_id

<%= link_to "Import", channel_import_path(:channel_id => index) %>

当你有routes with params时,你必须按路线文件中的描述传递参数。因此,根据您的路线,您需要发送channel_id param

channel_import GET    /channels/:channel_id/import(.:format)    channels#import

你可能从中受益的是发送params的能力。这可以通过括号来实现:

channel_import GET    /channels(/:channel_id)/import(.:format)    channels#import