ActionController :: UrlGenerationError:没有路由匹配{:action =>“ show”,:controller =>“ demographic_detail”,:id => nil}

时间:2018-07-06 12:23:14

标签: ruby-on-rails

我是编程新手,很抱歉,如果这是一个愚蠢的问题。我正在用Ruby on Rails编写控制器测试。该测试需要一个参数,但是我不确定该怎么做。当我使用rspec运行测试时,出现标题中显示的错误。

这是控制器代码的一部分:

class DemographicDetailController < ApplicationController

  #before_filter :authorize
  include ::Demographic

  def show

  @cinema_id = params[:cinema_id]
  @cinema = Cinema.find(@cinema_id) 
  @cinema_name = @cinema.name
  @cinmea_address = @cinema.address.full_street_address 
  @cinema_authority_id = @cinema.address.post_code.local_authority_id
  @c_working_age = Population.where(:local_authority_id =>  @cinema_authority_id , :year => Population.maximum("year")).first
  @c_working_age = @c_working_age.working_age_16_to_64

  @c_total_population = totalpopulation(@cinema_authority_id)
  @c_average_income = latestaverageincome(@cinema_authority_id)


  @all_cinemas = Cinema.all

  (...)

  end

这是我为show方法编写的测试:

describe "Demographic" do
  context "when testing the DemographicDetail controller" do
      #let(:params) { { disabled: false } }
      let(:cinema_id) { create(:cinema) }

it "should show demoraphic details successfully" do

  get :show, params: { id: @cinema_id }  
  assert_response :success

     end

end

这是路线:

   controller :demographic_detail do
    get '/demographic_detail/show/:cinema_id' => :show
  end

2 个答案:

答案 0 :(得分:0)

尝试一下

将路线修改为

get '/demographic_detail/show' => 'demographic_detail#show'

答案 1 :(得分:0)

尝试一下:

get '/demographic_details/:cinema_id' => 'demographic_detail#show'

controller :demographic_detail do
    get '/demographic_detail/:cinema_id' => :show
end