簡體   English   中英

設計和Rails的路線重定向問題

[英]Route redirecting issue with devise and rails

我的用戶模型與我的has_one關聯。 我在這里嘗試做的事情很簡單,但是我很難理解這里出了什么問題。 因此,由於我的模型具有has_one關聯,因此我只是想如果用戶已經嘗試創建“與"localhost3000/model/new"關聯的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

這個錯誤對我來說沒有多大意義,因為它說這是“新”控制器中的錯誤,該錯誤會將其呈現到編輯路徑,因此不會為零

在此處輸入圖片說明

發生這種情況是因為您在重定向到“編輯”時未設置@business 嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM