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
回答2件
あなたの回答
tips
プレビュー