error
1Started GET "/ugblogs/find" for ::1 at 2019-11-05 16:41:19 +0900 2Processing by UgblogsController#show as HTML 3 Parameters: {"id"=>"find"} 4 Ugblog Load (1.6ms) SELECT "ugblogs".* FROM "ugblogs" WHERE "ugblogs"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]] 5 ↳ app/controllers/ugblogs_controller.rb:72 6Completed 404 Not Found in 122ms (ActiveRecord: 15.6ms) 7 8 9 10ActiveRecord::RecordNotFound (Couldn't find Ugblog with 'id'=find): 11 12app/controllers/ugblogs_controller.rb:72:in `set_ugblog'
controller
1class UgblogsController < ApplicationController 2 layout 'ugblogs' 3 before_action :set_ugblog, only: [:show, :edit, :update, :destroy] 4 5 # GET /ugblogs 6 # GET /ugblogs.json 7 def index 8 @ugblogs = Ugblog.all.order('created_at desc').limit(3) 9 end 10 11 # GET /ugblogs/1 12 # GET /ugblogs/1.json 13 def show 14 end 15 16 # GET /ugblogs/new 17 def new 18 @ugblog = Ugblog.new 19 end 20 21 # GET /ugblogs/1/edit 22 def edit 23 end 24 25 def search 26 @ugblogs = Ugblog.search(params[:search]) 27 end 28 29 # POST /ugblogs 30 # POST /ugblogs.json 31 def create 32 @ugblog = Ugblog.new(ugblog_params) 33 34 respond_to do |format| 35 if @ugblog.save 36 format.html { redirect_to @ugblog, notice: 'Ugblog was successfully created.' } 37 format.json { render :show, status: :created, location: @ugblog } 38 else 39 format.html { render :new } 40 format.json { render json: @ugblog.errors, status: :unprocessable_entity } 41 end 42 end 43 end 44 45 # PATCH/PUT /ugblogs/1 46 # PATCH/PUT /ugblogs/1.json 47 def update 48 respond_to do |format| 49 if @ugblog.update(ugblog_params) 50 format.html { redirect_to @ugblog, notice: 'Ugblog was successfully updated.' } 51 format.json { render :show, status: :ok, location: @ugblog } 52 else 53 format.html { render :edit } 54 format.json { render json: @ugblog.errors, status: :unprocessable_entity } 55 end 56 end 57 end 58 59 # DELETE /ugblogs/1 60 # DELETE /ugblogs/1.json 61 def destroy 62 @ugblog.destroy 63 respond_to do |format| 64 format.html { redirect_to ugblogs_url, notice: 'Ugblog was successfully destroyed.' } 65 format.json { head :no_content } 66 end 67 end 68 69 private 70 # Use callbacks to share common setup or constraints between actions. 71 def set_ugblog 72 73 @ugblog = Ugblog.find(params[:id]) 74 end 75 76 # Never trust parameters from the scary internet, only allow the white list through. 77 def ugblog_params 78 params.require(:ugblog).permit(:title, :subtitle, :Text, :img, :img_cache, :remove_img) 79 end 80end
model
1 mount_uploader :img, ImgUploader 2 3 validates :title, :subtitle, :Text, :img, presence: {message:'は、必須事項です。'} 4 def self.search(search) 5 return Ugblog.all unless search 6 Ugblog.where(['content LIKE ?', "%#{search}%"]) 7 end 8end
html
1<p>検索</p> 2<%= form_tag(search_path,:method => 'get') do %> 3 <%= text_field_tag :search %> 4 <%= submit_tag 'Search', :name => nil %> 5<% end %> 6 7<ul><% @ugblogs.each do |post| %> 8 <li> 9 <%= link_to ugblog.title, post %> 10 <%= ugblog.created_at %> 11 <%= ugblog.status %> 12 </li> 13 14 <% end %> 15</ul>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。