Cloud9で作成したRailsのブログアプリをherokuにデプロイした後に、
投稿(Contact)をお気に入りしようとしたらエラーになってしまいました・・・
Cloud9上では正常に動作します。
The page you were looking for doesn't exist.
ActiveRecord::RecordNotFound (Couldn't find Contact with 'id'=):
該当のソースコード
html
1<div class="container"> 2 <div class="row"> 3 <div class="col-lg-12"> 4 5 <h1>問い合わせ詳細</h1> 6 <div class="col-md-6"> 7 <p>件名:<%= @contact.title %></p> 8 <p>内容:<%= @contact.content %></p> 9 <p><%= image_tag @contact.image %></p> 10 11 <% unless @contact.user_id == current_user.id %> 12 <% if @favorite.present? %> 13 <% if @favorites.user_id == current_user.id %> 14 <%= link_to 'お気に入り解除する', favorite_path(id: @favorite.id), 15 method: :delete, class: 'btn btn-default btn-sm btn btn-danger' %> 16 <% else %> 17 <%= link_to 'お気に入りにする', favorites_path(contact_id: @contact.id), 18 method: :post, class: 'btn btn-default btn-sm btn btn-primary' %> 19 <% end %> 20 <% else %> 21 <% if @favorites.nil? %> 22 <%= link_to 'お気に入りにする', favorites_path(contact_id: @contact.id), 23 method: :post, class: 'btn btn-default btn-sm btn btn-primary' %> 24 <% end %> 25 <% end %> 26 <% end %> 27 28 29 30 </div> 31 <div class="col-md-6"> 32 <h3>コメント一覧</h3> 33 <div id="responses_area"> 34 <%= render partial: 'responses/index', locals: { responses: @responses, contact: @contact } %> 35 </div> 36 <%= render partial: 'responses/form', locals: { response: @response, contact: @contact } %> 37 </div> 38 39 <div class="col-md-6"> 40 <%= link_to "一覧画面にもどる", contacts_path, class: 'btn btn-default btn-sm btn btn-default' %> 41 </div> 42 </div> 43 </div> 44</div>
FavoritesController
1class FavoritesController < ApplicationController 2 def create 3 favorite = current_user.favorites.create(contact_id: params[:contact_id]) 4 redirect_to contacts_url, notice: "#{favorite.contact.user.name}さんの投稿をお気に入りしました" 5 end 6 7 def destroy 8 favorite = current_user.favorites.find_by(id: params[:id]).destroy 9 redirect_to contacts_url, notice: "#{favorite.contact.user.name}さんの投稿をお気に入り解除しました" 10 end 11 12 def show 13 @user = User.find(session[:user_id]) 14 @favorite_contacts = @user.favorite_contacts 15 end 16 17 18 #追加 19 def index 20 @user = current_user 21 @favorites = Favorite.where(user_id: @user.id).all 22 end 23 24 def show_favorites 25 @contacts = Contact.find(params[:id]) 26 @favorites = Favorite.where(contact_id: @contact.id).all 27 end 28 private 29 30 def contact_params 31 params.require(:contact).permit(:title,:content, :image, :image_cache) 32 end 33end
ContactsController
1class ContactsController < ApplicationController 2 before_action :set_contact, only:[:show, :edit, :update, :destroy] 3 before_action :login_check, only: [:index, :new, :edit, :show, :destroy] 4 5 def index 6 @contacts = Contact.all 7 end 8 9 def new 10 if params[:back] 11 @contact = Contact.new(contact_params) 12 else 13 @contact = Contact.new 14 end 15 end 16 17 def create 18 @contact = Contact.new(contact_params) 19 @contact.user_id = current_user.id 20 if @contact.save 21 #ContactMailer.contact_mail(@contact).deliver 22 redirect_to contacts_path, notice: "問い合わせ中・・・" 23 else 24 render 'new' 25 end 26 end 27 28 def show 29 @contact = Contact.find(params[:id]) 30 31 @favorite = current_user.favorites.find_by(contact_id: @contact.id) 32 @favorites = Favorite.find_by(contact_id: @contact.id) 33 34 35 @responses = @contact.responses.includes(:user).all 36 @response = @contact.responses.build(user_id: current_user.id) if current_user 37 end 38 39 def edit 40 @contact = Contact.find(params[:id]) 41 end 42 43 def update 44 @contact = Contact.find(params[:id]) 45 if @contact.update(contact_params) 46 redirect_to contacts_path, notice: "投稿内容を更新しました" 47 else 48 render 'edit' 49 end 50 end 51 52 def destroy 53 @contact.destroy 54 redirect_to contacts_path, notice: "投稿を削除しました" 55 end 56 57 def confirm 58 @contact = Contact.new(contact_params) 59 @contact.user_id = current_user.id 60 render :new if @contact.invalid? 61 end 62 63 64 private 65 66 def contact_params 67 params.require(:contact).permit(:title,:content, :image, :image_cache) 68 end 69 70 def set_contact 71 @contact = Contact.find(params[:id]) 72 end 73 74 def login_check 75 unless current_user 76 render new_session_path 77 end 78 end 79end
model
1class Contact < ApplicationRecord 2 validates :title, presence: true, length: {in: 1..500 } 3 validates :content, presence: true, length: {in: 1..500} 4 5 belongs_to :user 6 has_one :favorite 7 has_many :favorite_users, through: :favorites, source: :user 8 has_many :responses, dependent: :destroy 9 10 mount_uploader :image, ImageUploader 11end
model
1class User < ApplicationRecord 2 validates :name, presence: true, length: { maximum: 30 } 3 validates :email, presence: true, length: { maximum: 255 }, 4 format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i } 5 6 before_validation { email.downcase! } 7 8 has_secure_password 9 validates :password, presence: true, length: { minimum: 6} 10 11 has_many :contacts 12 has_many :favorites, dependent: :destroy 13 has_many :favorite_contacts, through: :favorites, source: :contact 14 15 has_many :responses 16 17 mount_uploader :icon, IconUploader 18end
route
1Rails.application.routes.draw do 2 3 root to: 'tops#top' 4 resources :contacts do 5 resources :responses 6 collection do 7 post :confirm 8 end 9 end 10 11 resources :contacts do 12 member do 13 get "show_favorites" => "favorites#show_favorites" 14 end 15 end 16 resources :sessions, only:[:new, :create, :destroy] 17 resources :users 18 resources :favorites, only:[:create, :destroy, :show, :index, :show_favorites] 19 20end
試したこと
rake assets:precompile
<%= link_to 'お気に入りにする', favorites_path(contact_id: @contact.id),
を
<%= link_to 'お気に入りにする', contacts_path(contact_id: @contact.id),
に変えるなどしましたが全くダメでした・・・
もしも情報が不足していたらご指摘ください。
あなたの回答
tips
プレビュー