设计和 rails 的路由重定向问题

Route redirecting issue with devise and rails

我的模型与我的用户有 has_one 关联。我想在这里做的很简单,但我很难理解这里出了什么问题。因此,由于我与我的模型有 has_one 关联,在我看来,我只是在想,如果用户已经创建了与 has_one 关联关联的模型,如果他尝试访问 "localhost3000/model/new" 我会将他重定向到该特定模型的编辑页面。这是我所拥有的,但它告诉我它没有按预期工作。好像我的 if 语句没有捕捉到任何东西

class BusinessesController < ApplicationController
  before_action :set_business, only: [:show, :edit, :update, :destroy]

  def index
    @businesses = Business.all
    @buzzs = Buzz.all
  end

  def show
  end

  def new
    if current_user.business.nil?
      @business = current_user.build_business
    else
      render 'edit'
    end
  end

  def edit
  end

  def create
    @business = current_user.build_business(business_params)

    if @business.save
      redirect_to @business, notice: 'Business was successfully created.' 
    else
      render "new"
    end
  end

这个错误对我来说没有多大意义,因为它说它是 "new" 控制器中的一个错误,该控制器会将其呈现到编辑路径,因此不是 nil

发生这种情况是因为您在重定向到 'edit' 时没有设置 @business。试试这个:

def new 
 if current_user.business.nil? 
    @business = current_user.build_business 
 else 
    @business = current_user.business 
    render 'edit' 
 end 
end