Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 路由嵌套资源并将_重定向到控制器上的_Ruby On Rails_Ruby_Rspec_Rspec Rails - Fatal编程技术网

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

Ruby on rails 路由嵌套资源并将_重定向到控制器上的,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,我有一个控制器,当创建和编辑重定向到显示页面。我正在使用嵌套的atributes,无法使用重定向到显示路径。我的控制器是: class SalesmenController < ApplicationController def show authorize! :read, @salesman end def create @salesman = Salesman.new(params_salesman) authorize! :create, @s

我有一个控制器,当创建和编辑重定向到显示页面。我正在使用嵌套的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
我正在使用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]

有人能帮忙吗?

您需要返回重定向,而不是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
顺便说一句,您不需要在FG中创建记录。你可以为(:saller)做
attributes\u,你可以在帖子中直接使用


快乐的黑客

您需要返回重定向,而不是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
顺便说一句,您不需要在FG中创建记录。你可以为(:saller)
attributes\u,你可以在帖子中直接使用


快乐的黑客

运行
rake路由
run
rake路由
谢谢!是的,但是当我通过测试时仍然有相同的错误信息。@DenisPolicarpo我更新了答案。您需要将
公司
对象提供给URL帮助器。根据应用程序的工作方式,它可能是
@公司
当前公司
。您可以检查plsI是否放置@company=current_owner.companys.find(params[:company_id])并将_重定向到company_saller_路径(@company,@saller.id),但现在当我运行测试时,我遇到了一个错误:Failure/error:expect(response)。要使_http_status(:success)预期响应具有成功状态代码(2xx)但结果是正确的,因为302是重定向。你的测试是错误的。您需要更新它以测试重定向,而不是成功。谢谢!我现在明白了!非常感谢。是的,但是当我通过测试时仍然有相同的错误信息。@DenisPolicarpo我更新了答案。您需要将
公司
对象提供给URL帮助器。根据应用程序的工作方式,它可能是
@公司
当前公司
。您可以检查plsI是否放置@company=current_owner.companys.find(params[:company_id])并将_重定向到company_saller_路径(@company,@saller.id),但现在当我运行测试时,我遇到了一个错误:Failure/error:expect(response)。要使_http_status(:success)预期响应具有成功状态代码(2xx)但结果是正确的,因为302是重定向。你的测试是错误的。您需要更新它以测试重定向,而不是成功。谢谢!我现在明白了!
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