VerificationsController中的NoMethodError #create(has_one关系)

时间:2016-06-17 19:30:26

标签: ruby-on-rails ruby forms controller routes

我正在使用has_one关系并收到错误消息。 create方法未使用create来查找参数。

错误日志:

NoMethodError (undefined method `create' for #<Verification:0x007fcb71e2bd98>):
  app/controllers/verifications_controller.rb:7:in `create'

verificationsController:

def create
    @user = User.find(params[:user_id])
    @verification = current_user.verification.create(verification_params)
end


private
        def verification_params
            params.require(:verification).permit(:user_id, :paypal_firstname, :paypal_lastname, :paypal_email, :paypal_verified)
        end

路线:

devise_for :users ,
       :path => '' ,
       :path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
       :controllers => {:omniauth_callbacks => 'omniauth_callbacks',
                :registrations => 'registrations', :verifications => 'verifications'
              }


 resources :users, only: [:index, :show] do

 end

resources :users do
  resources :verifications, only: [:create]
end

get '/users/:id/paypal_verification' => 'users#paypal_verification'
patch '/users/:id/paypal_verification' => 'users#paypal_verification'

usersController:

def paypal_verification
    # @verification = @user.verification.new
    @user = current_user
@verification = Verification.create(params[:verification])
@user.verification = @verification
end

1 个答案:

答案 0 :(得分:1)

尝试current_user.create_verification(verification_params)

此处有更多详情:Rails: create on has_one association