レコードの複数登録を行いたく、試しにscaffoldで作ったサイトに、insert_allを用いて
まとめてフォーム画面から登録できるようにしたいのですが
insert_allはこの場合どこに使えば、使えるようになるのでしょうか?
Searchモデル titleカラム urlカラム
でやっています。
教えて頂けると幸いです。
controller
1class Maryu::SearchesController < Maryu::Base 2 before_action :set_search, only: [:show, :edit, :update, :destroy] 3 4 # GET /searches 5 # GET /searches.json 6 def index 7 @searches = Search.all 8 end 9 10 # GET /searches/1 11 # GET /searches/1.json 12 def show 13 end 14 15 # GET /searches/new 16 def new 17 @search = Search.new 18 end 19 20 # GET /searches/1/edit 21 def edit 22 end 23 24 # POST /searches 25 # POST /searches.json 26 def create 27 @search = Search.new(search_params) 28 29 respond_to do |format| 30 if @search.save 31 format.html { redirect_to "maryu/searches", notice: 'Search was successfully created.' } 32 format.json { render :show, status: :created, location: @search } 33 else 34 format.html { render :new } 35 format.json { render json: @search.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /searches/1 41 # PATCH/PUT /searches/1.json 42 def update 43 respond_to do |format| 44 if @search.update(search_params) 45 format.html { redirect_to @search, notice: 'Search was successfully updated.' } 46 format.json { render :show, status: :ok, location: @search } 47 else 48 format.html { render :edit } 49 format.json { render json: @search.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /searches/1 55 # DELETE /searches/1.json 56 def destroy 57 @search.destroy 58 respond_to do |format| 59 format.html { redirect_to searches_url, notice: 'Search was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 def search 64 if params[:title].present? 65 @searches = Search.where('title LIKE ?', "%#{params[:title]}%") 66 else 67 @searches = Search.none 68 end 69 end 70 private 71 # Use callbacks to share common setup or constraints between actions. 72 def set_search 73 @search = Search.find(params[:id]) 74 end 75 76 # Only allow a list of trusted parameters through. 77 def search_params 78 params.require(:search).permit(:title, :url) 79 end 80end 81
form
1<%= form_with(model: search, local: true) do |form| %> 2 <% if search.errors.any? %> 3 <div id="error_explanation"> 4 <h2><%= pluralize(search.errors.count, "error") %> prohibited this search from being saved:</h2> 5 6 <ul> 7 <% search.errors.full_messages.each do |message| %> 8 <li><%= message %></li> 9 <% end %> 10 </ul> 11 </div> 12 <% end %> 13 14 <div class="field"> 15 <%= form.label :title %> 16 <%= form.text_field :title,:size=>"60" %> 17 </div> 18 19 <div class="field"> 20 <%= form.label :url %> 21 <%= form.text_field :url,:size=>"60" %> 22 </div> 23 24 <div class="actions"> 25 <%= form.submit %> 26 </div> 27<% end %> 28
Model
1class Search < ApplicationRecord 2end 3
現在
やりたいイメージ
ひとつひとつ登録するだと面倒なので、
回答1件
あなたの回答
tips
プレビュー