1

我正在NoMethodError采取new行动business_controller

在我看来,它似乎正在访问@business表单的对象,然后发生错误:

undefined method `businesses_path' for

这是我的新方法:

def new
  if Business.where(:user_id => current_user.id).first.blank?
    @business = Business.new
  else
    redirect_to user_businesses_path(current_user.id)
  end
end

我的路线是:

  resources :users do
    resources :businesses
      member do
        get 'account'
        post 'payment'
        put 'update_password'
        get 'membership'
      end
  end

mind.blank 的建议更改

before_filter :check_if_user_has_business, :only => :index

   def new
      @business = Business.new
   end

  private
  def check_if_user_has_business
    redirect_to new_user_business_path(current_user) unless current_user.business
  end
4

1 回答 1

1

你有路线businesses_path还是只有路线user_businesses_path?如果您只有第二个,那么您应该在表单中指定该 URL:

<%= form_for @business, url: user_businesses_path do |f| %>

此外,如果您设置了正确的关联,那么您可以编写if如下声明:

if current_user.business.nil? # since it's a has_one association

我会这样写:

Class BusinessesController < ApplicationController
  before_filter :check_if_user_has_business, only: :new

  def new
    @business = Business.new
  end

  private

  def check_if_user_has_business
    redirect_to user_businesses_path(current_user) if current_user.business
  end
end
于 2013-05-22T05:02:08.013 回答