Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activeadmin render 'new' gives Couldn't find User without an id when trying to override the create controller method

I'm trying to create a page in activeadmin where a user can go in and create new user accounts.

I'm overriding the default create method for my User model with the code below.

I'm getting the error Couldn't find User without an ID when I try to render the new page.

Why would I be getting this error when trying to re-render the new action?

ActiveAdmin.register User do

  permit_params do
    permitted = [:email, :encrypted_password]
    permitted << :admin if current_user.is_admin?
    permitted
  end

  # We're overriding the new and edit controller methods to properly create users with devise. Otherwise the passwords don't get encrypted
  controller do

      def create
        user = User.new
        user.name = params[:user][:name]
        user.email = params[:user][:email]
        user.admin = params[:user][:admin]
        user.password = params[:user][:encrypted_password]
        user.password_confirmation = params[:user][:encrypted_password]

        if user.save
          redirect_to admin_user_path(user)
        else
          flash.now[:error] = user.errors.full_messages
          render 'new'     # THIS CAUSES THE ERROR "Couldn't find User without an ID"
          #redirect_to new_admin_user_path # This redirect works just fine
        end
      end

    end

end

Logs:

Started GET "/admin/users/new" for 127.0.0.1 at 2014-03-09 21:34:35 -0500
Processing by Admin::UsersController#new as HTML
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (31.1ms)
Completed 200 OK in 37ms (Views: 34.1ms | ActiveRecord: 0.4ms)


Started GET "/admin/users/new" for 127.0.0.1 at 2014-03-09 21:34:42 -0500
Processing by Admin::UsersController#new as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (32.7ms)
Completed 200 OK in 60ms (Views: 35.4ms | ActiveRecord: 4.3ms)


Started POST "/admin/users" for 127.0.0.1 at 2014-03-09 21:34:44 -0500
Processing by Admin::UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Aa6TBt0LADDcKAHs+gFokQroSVgTnxtlgLwzvCovIcs=", "user"=>{"name"=>"", "email"=>"", "encrypted_password"=>"[FILTERED]", "admin"=>"0"}, "commit"=>"Create User"}
  User Load (0.6ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (14.6ms)
Completed 500 Internal Server Error in 25ms

ActiveRecord::RecordNotFound - Couldn't find User without an ID:
like image 776
Catfish Avatar asked Mar 10 '14 02:03

Catfish


1 Answers

If someone is still looking at this then adysson's answer was quite helpful for me.

if user.save
  # DO YOUR JOB
  redirect_to admin_user_path(user)
else
  @resource = user
  render :new
end

Doing this will show resource errors under the input fields instead of flash message.

like image 120
Reinis Avatar answered Oct 17 '22 00:10

Reinis