ruby on rails - Resolving UsersController#show Couldn't find User with 'id'=1when that id was previously deleted -


i following michael hartl's tutorial. following error:

activerecord::recordnotfound in userscontroller#show couldn't find user 'id'=1  extracted source (around line #155)     record = s.execute([id], self, connection).first     unless record       raise recordnotfound, "couldn't find #{name} '#{primary_key}'=#{id}"     end     record   rescue rangeerror 

already have tried bundle install , restarted server because initial error related bcrypt gemfile (using v 3.1.7).

running rails console , inputing user.find(1) yields no user. however, user.all in rails c reveal have 1 user has id: 2 (the first user made deleted during prior test runs).

app/controllers/users_controller.rb

class userscontroller < applicationcontroller   def show  @user = user.find(params[:id])  end   def new  end end 

app/config/routes.rb rails.application.routes.draw do

  'users/new'    root 'static_pages#home'   'help' => 'static_pages#help'   'about' => 'static_pages#about'   'contact' => 'static_pages#contact'   'signup' => 'users#new'   resources :users  end 

currently url/users/2 displays expected debug showing:

 --- !ruby/hash:actioncontroller::parameters  controller: users  action: show  id: '2' 

however, not case id: 1 since has been destoryed.

is possible reassign id in database after has been created (i.e. prior id 1 or other arbitrary assignment)?

thanks!

there's no need care data have deleted database. , you'll never need visit "show" view type urls in browser. example, may want create index view, showing users.

class userscontroller < applicationcontroller   def index     @users = user.all   end    def show     @user = user.find(params[:id])   end    def new   end end  <% # "app/views/users/index.html.erb" %> <% @users.each |user| %>   <%= link_to "user.name", users_path(user) %> <% end %> 

when click links in index page, rails automatically find user's id , redirect right page.

if want type url "yourlocalhost/users/1" in browser, it's reasonable error mentioned. instead of trying make url working expected, you'd better add error handling. example, rescue error redirect users page, show them notice: "sorry man, page not exist, please visit other page. :p"

generally, it's not idea reuse ids in database have freed deleting record. because it's easy cause problems. example, if have other tables in database, "user_id" column. may have conflicts.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -