前提・実現したいこと
Ruby on ralsで投稿機能を作成中
発生している問題・エラーメッセージ
エラーがなにも出ない
chinema_nameカラムだけデータが表示される。
該当のソースコード
Route.rb
Rails.application.routes.draw do root to: "home#top" devise_for :users resources :users resources :items do resource :likes, only: [:create, :destroy] resources :item_comments, only: [:create, :destroy, :index] end resources :chinemas end
ChinemasController
class ChinemasController < ApplicationController before_action :authenticate_user!, except: [:index] def index @chinemas = Chinema.all.order(id: "DESC") end def new @chinema = Chinema.new end def create binding.pry @chinema = current_user.chinemas.build(chinema_params) if @chinema.save redirect_to chinema_path(@chinema), notice: "レシピを投稿しました。" else render :new end end def show @chinema = Chinema.find(params[:id]) end def edit @chinema = Chinema.find(params[:id]) if @chinema.user != current_user redirect_to chinemas_path, alert: "不正なアクセスです。" end end def update @chinema = Chinema.find(params[:id]) if @chinema.update(chinema_params) redirect_to chinema_path(@chinema), notice: "投稿を更新しました。" else render :edit end end def destroy chinema = Chinema.find(params[:id]) chinema.destroy redirect_to user_path(chinema.user), notice: "投稿を削除しました。" end private def chinema_params params.require(:chinema).permit(:chinema_name, :chinem_body, :chinem_image) end end
ChinemaModel
class Chinema < ApplicationRecord belongs_to :user, optional: true attachment :chinema_image geocoded_by :address after_validation :geocode with_options presence: true do validates :chinema_name validates :chinema_body validates :chinema_image end end
UserModel
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable attachment :profile_image has_many :items, dependent: :destroy has_many :likes, dependent: :destroy has_many :liked_items, through: :likes, source: :item has_many :chinemas, dependent: :destroy has_many :item_comments, dependent: :destroy validates :username, presence: true def already_liked?(item) self.likes.exists?(item_id: item.id) end end
chinemas/_form.html.erb
<section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-6"> <%= form_for @chinema do |f| %> <div class="field"> <%= f.label :chinema_name, "名前", class: "label" %> <%= f.text_field :chinema_name, class: "input" %> </div> <div class="field"> <%= f.label :chinema_body, "感想", class: "label" %> <%= f.text_area :chinema_body, class: "textarea" %> </div> <div class="field"> <%= f.label :address, "住所", class: "label" %> <%= f.text_area :address, class: "input" %> </div> <div class="field"> <%= f.label :chinema_image, "写真", class: "label" %> <%= f.attachment_field :chinema_image, class: "input" %> </div> <%= f.submit '投稿', class: "button is-success" %> <% end %> </div> </div> </div> </section>
chinemas/show.html.erb
<section class="hero is-success"> <div class="hero-body"> <div class="container"> <h1 class="title"> 詳細 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-5"> <div class="card"> <div class="card-image"> <figure class="image is-4by3"> <%= attachment_image_tag @chinema, :chinema_image, fallback: "no-image.png" %> </figure> </div> <div class="card-content"> <div class="media"> <div class="media-content"> <p class="title is-4"><%= @chinema.chinema_name %> </p> </div> </div> <div class="content"> <table class="table is-narrow"> <tr> <th>感想</th> </tr> <tr> <td><%= simple_format @chinema.chinema_body %> </td> </tr> <tr> <td>いいね:</td> </tr> <tr> <td>投稿者:<%= @chinema.user.username %> </td> </tr> </table> <% if @chinema.user.id == current_user.id %> <%= link_to "編集画面へ", edit_chinema_path(@chinema), class: "button is-success" %> <% end %> </div> </div> </div> </div> <div class="card"> <div id="map"></div> <style> #map{ height: 550px; width: 600px; } </style> <script type="text/javascript"> function initMap() { var test ={lat: <%= @chinema.latitude %>, lng: <%= @chinema.longitude %>}; var map = new google.maps.Map(document.getElementById('map'), { zoom: 15, center: test }); var transitLayer = new google.maps.TransitLayer(); transitLayer.setMap(map); var contentString = '住所:<%= @chinema.address %>'; var infowindow = new google.maps.InfoWindow({ content: contentString }); var marker = new google.maps.Marker({ position:test, map: map, title: contentString }); marker.addListener('click', function() { infowindow.open(map, marker); }); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyBkI_thGt1jmgpsYsGf_Yl4WazAuv95TTs&callback=initMap"> </script> <div class="content"> <table class="table is-narrow"> <tr> <td><p class="title is-6">住所</p></td> </tr> <tr> <td><p><%= @chinema.address %></p></td> </tr> </table> </div> </div> </section>
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。