前提・実現したいこと
ruby on rails で初歩てきなの書籍アウトプットアプリを作成中です。
outputモデルと、userモデルと、libraryモデルを紐づけて投稿機能を実装しています。
### 解決したいこと
create アクションを起動させた時にDBに保存される
情報を入力して投稿ボタンを押すと次のページに飛ぶようにする
(現状→if文の定義によりelseの方に行ってしまう。)
該当のソースコード
app/controllers/libraries_controller.rb def new @library = Library.new end def create @library= Library.new(library_params) if @library.save redirect_to root_path else render :new end end private def library_params params.require(:library).permit(:title, :impressions, :category_id, :lank_id, :author, :syuppan, :read_id) end end
config/routes,rb Rails.application.routes.draw do devise_for :users root to: "outputs#index" resources :users, only: [:edit, :update] resources :libraries, only: [:new, :create] do resources :outputs, only: [:index, :create] end end
app/model/library,rb class Library < ApplicationRecord belongs_to :user has_many :outputs validates :title, :impressions, :category_id, :lank_id, :author, :syuppan, :read_id, presence: true end
db/migrate class CreateLibraries < ActiveRecord::Migration[6.0] def change create_table :libraries do |t| t.string :title, null: false t.text :impressions, null: false t.integer :category_id, null: false t.text :lank_id, null: false t.string :author, null: false t.integer :syuppan, null: false t.string :read_id, null: false t.string :outputs, null: false t.references :user, foreign_key: true t.timestamps end end end
app/views/libraries/new.html.erb <div class='chat-library-form'> <h1>書籍登録フォーム</h1> <%=form_with model: @library, local: true do |f|%> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :著書名, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :title, class: 'chat__library_name chat-library-form__input', placeholder: '著書名を入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :アウトプット, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :impressions, class: 'chat__library_name chat-library-form__input', placeholder: 'アウトプットを入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :カテゴリー, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :category_id, class: 'chat__library_name chat-library-form__input', placeholder: 'アウトプットを入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :ランク, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :lank_id, class: 'chat__library_name chat-library-form__input', placeholder: 'アウトプットを入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :著者, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :author, class: 'chat__library_name chat-library-form__input', placeholder: 'アウトプットを入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :出版社, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :syuppan, class: 'chat__library_name chat-library-form__input', placeholder: 'アウトプットを入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'> <%= f.label :読んだ場所, class: 'chat-library-form__label'%> </div> <div class='chat-library-form__field--right'> <%= f.text_field :read_id, class: 'chat__library_name chat-library-form__input', placeholder: 'アウトプットを入力してください'%> </div> </div> <div class='chat-library-form__field'> <div class='chat-library-form__field--left'></div> <div class='chat-library-form__field--right'> <%= f.submit class: 'chat-library-form__action-btn'%> </div> </div> <% end %> </div>
登録ボタンを押した後のコンソール
(2.5ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 (0.4ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC Processing by LibrariesController#create as HTML Parameters: {"authenticity_token"=>"kGujcXLmPiMvI28HP/bTLbOOLabiRkLkpiqElu726SQ1HT+U/KzJLQ3OScCgYq+rK+naGQTUF28LMDVaJn2sEg==", "library"=>{"title"=>"aaa", "impressions"=>"aaa", "category_id"=>"aaa", "lank_id"=>"aaa", "author"=>"aaa", "syuppan"=>"aaa", "read_id"=>"aaa"}, "commit"=>"Create Library"} User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 Rendering libraries/new.html.erb within layouts/application Rendered libraries/new.html.erb within layouts/application (Duration: 15.5ms | Allocations: 3394) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 1180ms (Views: 1087.9ms | ActiveRecord: 10.1ms | Allocations: 1561154)
試したこと
他のコードを元に行っている為、クラス名等は少し異なります。
また、category_id 、lank_id、read_idは今後integer型を使い実装していきます。
def create binding.pry→①gem導入済み @library= Library.new(library_params) if @library.save redirect_to root_path else render :new end end → params => <ActionController::Parameters {"authenticity_token"=>"qug7w4lniKwERgrbq0Fw2+r/qtn5L0p0E1yepByr69sPnqcmBy1/oiarLBw01QxdcphdZh+9H/++Ri9o1CCu7Q==", "library"=>{"title"=>"aaa", "impressions"=>"aaa", "category_id"=>"aaa", "lank_id"=>"aaa", "author"=>"aaa", "syuppan"=>"aaa", "read_id"=>"aaa"}, "commit"=>"Create Library", "controller"=>"libraries", "action"=>"create"} permitted: false> → library_params => <ActionController::Parameters {"title"=>"aaa", "impressions"=>"aaa", "category_id"=>"aaa", "lank_id"=>"aaa", "author"=>"aaa", "syuppan"=>"aaa", "read_id"=>"aaa"} permitted: true>
回答3件
あなたの回答
tips
プレビュー