質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

363閲覧

ruby on rails の登録時、連番に不要な数字が入ってしまいます。

pyoo001213

総合スコア13

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2019/05/02 20:45

編集2019/05/03 05:44

rails 初心者です。
教則本を見ながらruby on rails で日記データの登録を行いました。
「日記を新規作成」リンクを押下するとタイトルと本文を登録し、自動的にIDが採番できる
仕組みになっています。

日記を新規作成から登録をすると登録自体はできるのですが、IDに「98019096」がついてしまいます。
連番にはなっていますが、どうしてこのような数字がついてしまうのか悩んでおります。
文字コードはUTF8で保存しています。色々ググりましたが解決が出来ませんでした。

該当のファイルのURLの末尾がIDとリンクしています。
どこを見ればいいかわかりませんが、怪しいファイル(_form.html.erb、diaries_controller.rb)を抜粋しました。
ご教示いただければ幸いです。

▼一覧画面   http://localhost:3000/diaries

イメージ説明

▼新規作成画面 http://localhost:3000/diaries/new

イメージ説明

▼登録後の画面 http://localhost:3000/diaries/980190971

イメージ説明

▼試した事
教則本通りに作成をしております。
指摘されたコードの見直しを行いました。

ruby 2.4.6p354
sqlite3 3.28.0

C:\Users\iqura555\sample>rails s => Booting Puma => Rails 5.2.3 application starting in development => Run `rails server -h` for more startup options *** SIGUSR2 not implemented, signal based restart unavailable! *** SIGUSR1 not implemented, signal based restart unavailable! *** SIGHUP not implemented, signal based logs reopening unavailable! Puma starting in single mode... * Version 3.12.1 (ruby 2.4.6-p354), codename: Llamas in Pajamas * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://localhost:3000 Use Ctrl-C to stop Started GET "/diaries/980190970" for ::1 at 2019-05-03 04:58:42 +0900 (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC ↳ C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 Processing by DiariesController#show as HTML Parameters: {"id"=>"980190970"} Diary Load (0.3ms) SELECT "diaries".* FROM "diaries" WHERE "diaries"."id" = ? LIMIT ? [["id", 980190970], ["LIMIT", 1]] ↳ app/controllers/diaries_controller.rb:67 Rendering diaries/show.html.erb within layouts/application Rendered diaries/show.html.erb within layouts/application (2.5ms) Completed 200 OK in 282ms (Views: 252.3ms | ActiveRecord: 1.1ms) Started GET "/diaries/980190970" for ::1 at 2019-05-03 04:58:43 +0900 Processing by DiariesController#show as HTML Parameters: {"id"=>"980190970"} Diary Load (0.2ms) SELECT "diaries".* FROM "diaries" WHERE "diaries"."id" = ? LIMIT ? [["id", 980190970], ["LIMIT", 1]] ↳ app/controllers/diaries_controller.rb:67 Rendering diaries/show.html.erb within layouts/application Rendered diaries/show.html.erb within layouts/application (2.5ms) Completed 200 OK in 83ms (Views: 70.9ms | ActiveRecord: 0.2ms) Started GET "/diaries/980190970/edit" for ::1 at 2019-05-03 04:58:47 +0900 Processing by DiariesController#edit as HTML Parameters: {"id"=>"980190970"} Diary Load (0.2ms) SELECT "diaries".* FROM "diaries" WHERE "diaries"."id" = ? LIMIT ? [["id", 980190970], ["LIMIT", 1]] ↳ app/controllers/diaries_controller.rb:67 Rendering diaries/edit.html.erb within layouts/application Rendered diaries/_form.html.erb (17.7ms) Rendered diaries/edit.html.erb within layouts/application (27.2ms) Completed 200 OK in 96ms (Views: 85.4ms | ActiveRecord: 0.2ms) Started PATCH "/diaries/980190970" for ::1 at 2019-05-03 04:58:48 +0900 Processing by DiariesController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"GgI9ibLy5Gq9Lp6im7M/eHN5HsebPqupHCgy2eiOBFZiFoUwLH2Q7vW3MMnRQ185oDw90pJUe92LNRhFfFQAKg==", "d iary"=>{"title"=>"TEST3", "body"=>"test4"}, "commit"=>"Update Diary", "id"=>"980190970"} Diary Load (0.2ms) SELECT "diaries".* FROM "diaries" WHERE "diaries"."id" = ? LIMIT ? [["id", 980190970], ["LIMIT", 1]] ↳ app/controllers/diaries_controller.rb:67 (0.1ms) begin transaction ↳ app/controllers/diaries_controller.rb:44 (0.0ms) commit transaction ↳ app/controllers/diaries_controller.rb:44 Redirected to http://localhost:3000/diaries/980190970 Completed 302 Found in 5ms (ActiveRecord: 0.3ms) Started GET "/diaries/980190970" for ::1 at 2019-05-03 04:58:48 +0900 Processing by DiariesController#show as HTML Parameters: {"id"=>"980190970"} Diary Load (0.2ms) SELECT "diaries".* FROM "diaries" WHERE "diaries"."id" = ? LIMIT ? [["id", 980190970], ["LIMIT", 1]] ↳ app/controllers/diaries_controller.rb:67

▼_form.html.erb

<%= form_with(model: diary, local: true) do |form| %> <% if diary.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(diary.errors.count, "error") %> prohibited this diary from being saved:</h2> <ul> <% diary.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= form.label :title %> <%= form.text_field :title, id: :diary_title %> </div> <div class="field"> <%= form.label :body %> <%= form.text_area :body, id: :diary_body %> </div> <div class="actions"> <%= form.submit %> </div> <% end %>

app/controllers/diaries_controller.rb

class DiariesController < ApplicationController before_action :set_diary, only: [:show, :edit, :update, :destroy] # GET /diaries # GET /diaries.json def index @diaries = Diary.all end # GET /diaries/1 # GET /diaries/1.json def show end # GET /diaries/new def new @diary = Diary.new end # GET /diaries/1/edit def edit end # POST /diaries # POST /diaries.json def create @diary = Diary.new(diary_params) respond_to do |format| if @diary.save format.html { redirect_to @diary, notice: 'Diary was successfully created.' } format.json { render :show, status: :created, location: @diary } else format.html { render :new } format.json { render json: @diary.errors, status: :unprocessable_entity } end end end # PATCH/PUT /diaries/1 # PATCH/PUT /diaries/1.json def update respond_to do |format| if @diary.update(diary_params) format.html { redirect_to @diary, notice: 'Diary was successfully updated.' } format.json { render :show, status: :ok, location: @diary } else format.html { render :edit } format.json { render json: @diary.errors, status: :unprocessable_entity } end end end # DELETE /diaries/1 # DELETE /diaries/1.json def destroy @diary.destroy respond_to do |format| format.html { redirect_to diaries_url, notice: 'Diary was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_diary @diary = Diary.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def diary_params params.require(:diary).permit(:title, :body) end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ockeghem

2019/05/03 01:08

「教則本」の書名、あるいはURL等を提示ください
pyoo001213

2019/05/03 05:45

ご連絡ありがとうございます。 ▼教則本 たった1日で基本が身に付く! Ruby on Rails 超入門 URLは各画面構成の横に追記しました。 ご確認のほど、よろしくお願いいたします。
siruku6

2019/05/06 04:33 編集

通常、IDを開発者がアプリ側で編集や指定することはないと考えられるので、railsの問題ではなく、DBや環境の問題なのではないでしょうか?? `$ bundle exec rails db:migrate:reset` で、一度DBを空っぽにしてみてはいかがでしょう? (このコマンドを実行すると、アプリに登録済みのデータがすべて消えるのでご注意ください。) 一度空っぽにして再発するようであれば、対策を考える必要はありそうですが、、、
pyoo001213

2019/05/06 17:22

ありがとうございました。 `$ bundle exec rails db:migrate:reset` このままではエラーが出たので bundle exec rake db:drop:_unsafe 後、 bundle exec rails db:migrate:reset でDBを全削除をして再度新規登録をしたらIDが1から連番になりました。 本当に難しいと言うか、、勉強をしなければいけませんね。 ご教示いただき、ありがとうございました。
guest

回答2

0

何かしら連番を崩れさせた根本的な原因があったのではないかと思いますが、現在railsを基礎から学習している最中であれば、まだ気にしなくてもよい問題だと私は思います。

再発するようだと困りますが、もう発生しないようであれば一旦忘れてもよいと思います。

投稿2019/05/07 12:56

siruku6

総合スコア1382

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

siruku6

2019/05/07 12:57

コメントするつもりが間違って回答に書いてしまいました。。。。 なにはともあれ、お役に立てて何よりです。
guest

0

自己解決

$ bundle exec rails db:migrate:reset
このままではエラーが出たので
bundle exec rake db:drop:_unsafe 後、 bundle exec rails db:migrate:reset
でDBを全削除をして再度新規登録をしたらIDが1から連番になりました。

siruku6様、有益なアドバイスを頂き誠にありがとうございました。

投稿2019/05/06 17:23

pyoo001213

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問