ruby on rails にてブログサイトを製作していますが、投稿を表示する(showアクション)においてエラーが出ます。
投稿は micropost モデル、投稿者(ユーザ)は user モデルを作成してあります。当然ながら、micropost は belong to user の関係にあります。
エラーの内容は
Missing template microposts/show, application/show with {:locale=>[:ja], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/ubuntu/workspace/my_blog/app/views" * "/usr/local/rvm/rubies/ruby-2.0.0-p598/lib/ruby/gems/2.0.0/gems/devise-3.4.1/app/views"
というものです。
あともう少しで完成、というところで困っています。よろしくお願いします。microposts/show に関連するモデル、コントローラー、ビュー、そして route.rb のソースを以下に貼り付けておきます。
app/models/micropost.rb
ruby
1class Micropost < ActiveRecord::Base 2 belongs_to :user, class_name: "User", foreign_key: "user_id" 3 4 STATUS_VALUES = %w(draft user_only public) 5 6 validates :title, presence: true, length: { maximum: 200 } 7 validates :content, presence: true, length: { maximum: 4000 } 8 default_scope -> { order('created_at DESC') } 9 validates :user_id, presence: true 10 validates :status, inclusion: { in: STATUS_VALUES } 11 12 scope :common, -> { where(status: "public") } 13 scope :published, -> { where("status <> ?", "draft") } 14 scope :full, ->(user) { 15 where("status <> ? OR user_id = ?", "draft", user.id) } 16 scope :readable_for, ->(user) { user ? full(user) : common } 17 18 scope :open, -> { 19 now = Time.current 20 where("posted_at <= ?", now) } 21 22 class << self 23 def status_text(status) 24 I18n.t("activerecord.attributes.micropost.status_#{status}") 25 end 26 27 def status_options 28 STATUS_VALUES.map { |status| [status_text(status), status] } 29 end 30 31 def sidebar_entries(user, num) 32 readable_for(user).order(posted_at: :desc).limit(num) 33 end 34 end 35 36end 37
app/controllers/microposts_controller.rb
ruby
1class MicropostsController < ApplicationController 2 before_action :authenticate_user! 3 4 # (中略) 5 6 # 記事詳細 7 def show 8 @micropost = Micropost.readable_for(current_user).find(params[:id]) 9 end 10 11 # (中略) 12 13 private 14 15 def micropost_params 16 params.require(:micropost).permit(:title, :content, :posted_at, :status) 17 end 18 19 def correct_user 20 @micropost = current_user.microposts.find_by(id: params[:id]) 21 redirect_to root_url if @micropost.nil? 22 end 23end
app/views/microposts/show.html.erb
html
1<% @page_title = @micropost.title + " - " + @micropost.user.name + "さんのブログ" %> 2<h1><%= @micropost.user.name %>さんのブログ</h1> 3<h2><%= @micropost.title %></h2> 4 5<% if current_member == @micropost.user %> 6 <p><%= link_to "編集", [:edit, @micropost] %></p> 7<% end %> 8 9<%= simple_format(@micropost.content) %> 10 11<%= render "footer", micropost: @micropost %>
app/config/routes.rb
ruby
1Rails.application.routes.draw do 2 devise_for :users, :controllers => { 3 :registrations => "registrations" 4 } 5 get "users/show" 6 7 resources :users do 8 collection { get "search" } 9 resources :microposts, only: [:index] 10 end 11 12 resources :users, only: [:show, :index, :destroy] 13 resources :microposts 14 resources :contacts, only: [:new, :create] 15 16 root 'top#index' 17 18 match '/help', to: 'static_pages#help', via: 'get' 19 match '/about', to: 'static_pages#about', via: 'get' 20 match '/contact', to: 'static_pages#contact', via: 'get' 21 match '/blog', to: 'microposts#index', via: 'get' 22end
あなたの回答
tips
プレビュー