Michael Lombardo Michael Lombardo - 5 months ago 12
Ruby Question

NoMethodError in VerificationsController#create (has_one relationship)

I'm using a

has_one
relationship and am getting an error. The
create
method isn't using
create
to find the params.

Error Log:

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


routes:

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

Answer

Try current_user.create_verification(verification_params).

More details here: Rails: create on has_one association

Comments