在控制器上路由嵌套资源和redirect_to

时间:2017-10-29 03:38:39

标签: ruby-on-rails ruby rspec rspec-rails

我在创建和编辑重定向到节目页面时有一个控制器。我正在使用嵌套的atrribututes,我无法使用重定向到节目的路径。我的控制器是这样的:

class SalesmenController < ApplicationController

  def show
    authorize! :read, @salesman
  end

  def create
    @salesman = Salesman.new(params_salesman)
    authorize! :create, @salesman
    if @salesman.save
      redirect_to company_salesman_path(@salesman.id)
      flash[:notice] = "Salesman saved!"
    else
      flash.now[:error] = "Could not create salesman!"
      render :new
    end
  end


  private

  def params_salesman
    params.require(:salesman).permit(:name, :company_id)
  end
end

我的路线是:

Rails.application.routes.draw do
  resources :companies do
    resources :salesmen
    resources :goals do
      resources :days
    end
  end
  devise_for :owners, :controllers => { registrations: 'registrations' }
end

我正在使用rspec进行测试,并收到以下错误消息:

 1) SalesmenController POST #create redirect to new team
     Failure/Error: redirect_to company_salesman_path(@salesman.id)

     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :company_id=>#<Salesman id: 310, name: "Raymond Kihn", company_id: 831, created_at: "2017-10-29 03:20:33", updated_at: "2017-10-29 03:20:33">, :controller=>"salesmen"} missing required keys: [:id]

我的测试是这样的:

require 'rails_helper'

RSpec.describe SalesmenController, type: :controller do
   include Devise::Test::ControllerHelpers

   before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:owner]
    @current_owner = FactoryGirl.create(:owner)
    sign_in @current_owner
    @current_company = FactoryGirl.create(:company, owner: @current_owner)
  end

  describe "POST #create" do
    before(:each) do
      salesman = FactoryGirl.create(:salesman, company: @current_company)
      post :create, params: {:company_id => @current_company.id, salesman: { name: salesman.name, company_id: @current_company.id } }
    end

    it "redirect to new team" do
      expect(response).to have_http_status(:success)
    end

    it "Create team with right attributes" do
      expect(Salesman.last.company).to eql(@current_company)
      expect(Salesman.last.name).to eql(@salesman[:name])
    end
  end
end

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您需要返回重定向,而不是flash消息。在Ruby中,在方法中执行的最后一行是返回的内容。您有一条Flash消息,因此从创建操作返回的内容不是重定向调用。

修复它,你应该很好!

class SalesmenController < ApplicationController
  # def index...

  def create
    @salesman = Salesman.new(params_salesman)
    authorize! :create, @salesman
    if @salesman.save
      flash[:notice] = "Salesman saved!"
      redirect_to company_salesman_path(current_company, @salesman)  # This has to executed last for the redirect
    else
      flash.now[:error] = "Could not create salesman!"
      render :new
    end

    # ....
  end
end

需要更新规范以反映重定向。成功就是渲染页面。

it "redirect to new team" do
  expect(response).to redirect_to(company_salesman_path(company, salesman))
end

顺便说一句,你不需要在FG中创建记录。您可以执行attributes_for(:salesman),您可以直接在帖子中使用。

快乐的黑客攻击!