「nested-form」を使い子テーブルの情報を保存したいのですがうまく保存されません。分かる方いましたらよろしくお願いします。
Rails ver は5.0.7.2です。
親:User テーブル (company,userid,pass,memo)
子:Schedule テーブル (time,user_id,category_id)
Category テーブル (category)
ruby
1#_form.html.erb 2 3 4<%= nested_form_for(@user) do |f| %> 5 6 <div class="field"> 7 <%= f.label "企業名" %> 8 <%= f.text_field :company,class:"form-control",placeholder:"企業名" %> 9 </div> 10 11 <div class="field"> 12 <%= f.label "ユーザーID" %> 13 <%= f.text_field :userid,class:"form-control",placeholder:"ユーザーID" %> 14 </div> 15 16 <div class="field"> 17 <%= f.label "パスワード" %> 18 <%= f.text_field :pass,class:"form-control",placeholder:"パスワード" %> 19 </div> 20 21 <div class="field"> 22 <%= f.label "メモ" %> 23 <%= f.text_field :memo,class:"form-control",placeholder:"メモ" %> 24 </div> 25 26 27 28 <%= f.fields_for :schedules do |f| %> 29 <hr class=hr-tag> 30 <div class="field"> 31 <%= f.collection_select :category_id, @category, :id, :name,{:prompt => "予定を選択"},class:"form-control",id:"select-form"%> 32 <%= f.text_field :time, class:"form-control",placeholder:"日時"%> 33 </div> 34 <div class="buttons"> 35 <%= f.link_to_remove do%> 36 <span class="glyphicon glyphicon-trash color-blue"></span> 37 </div> 38 39 <% end %> 40 <% end %> 41 42 43 <%= f.link_to_add "予定の追加", :schedules ,class:"button-4-1"%> 44 <%= f.submit "作成",class:"button-4-1"%> 45 46 47 48<% end %>
ruby
1#users_controller.rb 2 3 # GET /users 4 # GET /users.json 5 def index 6 @users = User.all 7 @schedules=Schedule.all 8 end 9 10 # GET /users/1 11 # GET /users/1.json 12 def show 13 end 14 15 # GET /users/new 16 def new 17 @user = User.new 18 @schedule=@user.schedules.build 19 @category=Category.all 20 end 21 22 # GET /users/1/edit 23 def edit 24 end 25 26 # POST /users 27 # POST /users.json 28 def create 29 @user = User.new(user_params) 30 31 respond_to do |format| 32 if @user.save 33 format.html { redirect_to @user, notice: 'User was successfully created.' } 34 format.json { render :show, status: :created, location: @user } 35 else 36 format.html { render :new } 37 format.json { render json: @user.errors, status: :unprocessable_entity } 38 end 39 end 40 end 41 42 # PATCH/PUT /users/1 43 # PATCH/PUT /users/1.json 44 def update 45 respond_to do |format| 46 if @user.update(user_params) 47 format.html { redirect_to @user, notice: 'User was successfully updated.' } 48 format.json { render :show, status: :ok, location: @user } 49 else 50 format.html { render :edit } 51 format.json { render json: @user.errors, status: :unprocessable_entity } 52 end 53 end 54 end 55 56 # DELETE /users/1 57 # DELETE /users/1.json 58 def destroy 59 @user.destroy 60 respond_to do |format| 61 format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 62 format.json { head :no_content } 63 end 64 end 65 66 private 67 # Use callbacks to share common setup or constraints between actions. 68 def set_user 69 @user = User.find(params[:id]) 70 end 71 72 # Only allow a list of trusted parameters through. 73 def user_params 74 params.require(:user).permit(:company, :userid, :pass, :memo,users_ttributes: [:id, :time, :user_id, :category_id,:_destroy ] ) 75 end 76end 77
ruby
1#user.rb 2class User < ApplicationRecord 3 has_many :schedules, dependent: :destroy 4 accepts_nested_attributes_for :schedules, allow_destroy: true 5end 6
ruby
1#schedule.rb 2class Schedule < ApplicationRecord 3 belongs_to :user 4end
ruby
1#category.rb 2class Category < ApplicationRecord 3end 4
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/13 15:01
2020/05/13 15:07
2020/05/13 15:11
2020/05/13 15:20
2020/05/13 15:32
2020/05/13 15:37
2020/05/13 15:49
2020/05/14 09:45