【问题标题】:Route redirecting issue with devise and rails设计和导轨的路由重定向问题
【发布时间】:2016-03-06 05:30:52
【问题描述】:

我的模型中与我的用户有一个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

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      相关资源
      最近更新 更多