質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

0回答

1511閲覧

ruby on rails でブログサイトを製作しています。投稿を表示する際に template is missing エラーがずっと出ます

shocyu

総合スコア17

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

1クリップ

投稿2015/09/28 15:35

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問