代码之家  ›  专栏  ›  技术社区  ›  Denis Policarpo

路由嵌套资源并将_重定向到控制器上的

  •  0
  • Denis Policarpo  · 技术社区  · 7 年前

    我有一个控制器,当创建和编辑重定向到显示页面。我使用嵌套的atributes,无法使用重定向到节目路径。我的控制器是:

    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 回复  |  直到 7 年前
        1
  •  2
  •   Ziyan Junaideen    7 年前

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

    修好它,你应该很好!

    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)

    快乐的黑客!