index から new ページに飛びたい
トップページから、新規投稿ページへアクセスする際、エラーが発生します。
発生している問題・エラーメッセージ
NoMethodError in HomesController#new undefined method `[]' for nil:NilClass
該当のソースコード
Homescontroller
1class HomesController < ApplicationController 2 before_action :authenticate_user! 3 4 def index 5 @homes = Home.includes(:user).order("created_at DESC") 6 end 7 8 def new 9 @home = Home.new 10 end 11 12 def create 13 @home = home.new(home_params) 14 if @home.save 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 private 22 23 def home_params 24 params.require(:home).permit(:home_name, :age, :name, :tel, :email, :prefecture_id, :class_id, :city, :address, :price, :rate, :rent, :management, images: []).merge(user_id: current_user.id) 25 end 26end 27
homesindexHTML
1<%= render "shared/header" %> 2<div class='main'> 3</div> 4<%= render "shared/footer"%>
sharedheaderHTMLerb
1<header class='top-page-header'> 2 <div class='search-bar-contents'> 3 <%= link_to image_tag("meiwa.png", class:"meiwa-icon"), "/" %> 4 </div> 5 <div class='nav'> 6 <ul class='lists-right'> 7 <%if user_signed_in? %> 8 <li>こんにちは、<%= link_to "#{current_user.lastname}さん", root_path, class: "user-firstname" %></li> 9 <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class: "logout"%></li> 10 <li><%= link_to image_tag("new.png", class:"new-home"), new_home_path %></li> 11 <% else %> 12 <li><%= link_to 'ログイン', new_user_session_path, class: "login" %></li> 13 <li><%= link_to '新規登録', new_user_registration_path, class: "sign-up" %></li> 14 <% end %> 15 </ul> 16 </div> 17</header>
modelHomerb
1class Home < ApplicationRecord 2 belongs_to :user 3 has_one_attached :image 4 5 extend ActiveHash::Associations::ActiveRecordExtensions 6 belongs_to :prefecture 7 belongs_to :class 8 9 with_options presence: true do 10 validates :home_name 11 validates :age 12 validates :name 13 validates :tel 14 validates :email 15 validates :prefecture_id 16 validates :class_id 17 validates :city 18 validates :address 19 validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999 } 20 validates :rate, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999 } 21 validates :rent, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999 } 22 validates :management, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999 } 23 validates :images 24 end 25 validates :prefecture_id, :class_id, numericality: { other_than: 1 } 26end
prefecturerb
1class Prefecture < ActiveHash::Base 2 self.data = [ 3 4 ] 5 6 include ActiveHash::Associations 7 has_many :homes 8end 9
classrb
1class Class < ActiveHash::Base 2 self.data = [ 3 4 5 include ActiveHash::Associations 6 has_many :homes 7end
routesrb
1Rails.application.routes.draw do 2 devise_for :users 3 root to: 'homes#index' 4 resources :homes 5end
試したこと
各モデル、アソシエーションの確認
テーブル名記述ミス確認
newアクションの仕組みがいまいち理解できていません。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
newHTML
1<div class="homes-sell-contents"> 2 <header class="homes-sell-header"> 3 <%= link_to image_tag('meiwa.png' , size: '185x50'), "/" %> 4 </header> 5 <div class="homes-sell-main"> 6 <h2 class="homes-sell-title">物件の情報を入力</h2> 7 <%= form_with(model: @home, local: true) do |f| %> 8 9 <%# インスタンスを渡して、エラー発生時にメッセージが表示されるようにしましょう。%> 10 <%= render 'shared/error_messages', model: f.object %> 11 <%# //インスタンスを渡して、エラー発生時にメッセージが表示されるようにしましょう。%> 12 13 <%# 出品画像 %> 14 <div class="img-upload"> 15 <div class="weight-bold-text"> 16 物件画像 17 <span class="indispensable">必須</span> 18 </div> 19 <div class="click-upload"> 20 <p> 21 クリックしてファイルをアップロード 22 </p> 23 <%= f.file_field :images, name: 'home[images][]', id:"home-image" %> 24 </div> 25 <div id= "home-box-img"> 26 </div> 27 </div> 28 <%# /物件画像 %> 29 30 <%# 物件名と築年数、最寄り徒歩時間 %> 31 <div class="new-homes"> 32 <div class="weight-bold-text"> 33 物件名 34 <span class="indispensable">必須</span> 35 </div> 36 <%= f.text_area :home_name, class:"homes-text", id:"home-name", placeholder:"商品名(必須 40文字まで)", maxlength:"40" %> 37 <div class="homes-explain"> 38 <div class="weight-bold-text"> 39 築年数 最寄り駅(徒歩〜分) 40 <span class="indispensable">必須</span> 41 </div> 42 <%= f.text_area :age, class:"homes-text", id:"item-info", placeholder:"築年数 〜年 最寄り駅(徒歩〜分)" ,rows:"7" ,maxlength:"1000" %> 43 </div> 44 </div> 45 <%# /物件名と築年数、最寄り徒歩時間 %> 46 47 <%# 担当者連絡先 %> 48 <div class="homes-detail"> 49 <div class = "name"> 50 <div class="weight-bold-text"> 51 担当者 52 <span class="indispensable">必須</span> 53 </div> 54 <div class="form"> 55 氏名 56 </div> 57 <%= f.text_area :name, class:"input-default", id:"name", placeholder:"例) furima太郎" %> 58 </div> 59 <div class="tel"> 60 <div class="weight-bold-text"> 61 電話番号 62 <span class="indispensable">必須</span> 63 </div> 64 <%= f.text_field :tel, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%> 65 </div> 66 67 <div class="email"> 68 <div class="weight-bold-text"> 69 メールアドレス 70 <span class="indispensable">必須</span> 71 </div> 72 <%= f.email_field :email, class:"input-default", id:"email", placeholder:"PC・携帯どちらでも可", autofocus: true %> 73 </div> 74 </div> 75 <%# /担当者連絡先 %> 76 77 <%# 所在地 %> 78 <div class="home-address"> 79 <div class="weight-bold-text"> 80 物件住所 81 <span class="indispensable">必須</span> 82 </div> 83 <div class="prefecture"> 84 <div class="weight-bold-text"> 85 物件の地域 <%#都道府県%> 86 <span class="indispensable">必須</span> 87 </div> 88 <%= f.collection_select(:prefecture_id, Prefecture.all, :id, :name, {}, {class:"select-box", id:"home-prefecture"}) %> 89 </div> 90 <div class="class"> 91 <div class="weight-bold-text"> 92 物件区域 <%#東京23区%> 93 <span class="indispensable">必須</span> 94 </div> 95 <%= f.collection_select(:class_id, Class.all, :id, :name, {}, {class:"select-box", id:"home-class"}) %> 96 </div> 97 <div class= "city"> 98 <div class="weight-bold-text"> 99 市町村 100 <span class="indispensable">任意</span> 101 </div> 102 <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%> 103 </div> 104 <div class="address"> 105 <div class="weight-bold-text"> 106 番地 107 <span class="indispensable">必須</span> 108 </div> 109 <%= f.text_field :address, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%> 110 </div> 111 </div> 112 <%# /所在地 %> 113 114 <%# 販売価格 %> 115 <div class="sell-price"> 116 <div class="weight-bold-text"> 117 販売価格 118 </div> 119 <div class="price-content"> 120 <div class="weight-bold-text"> 121 価格 122 <span class="indispensable">必須</span> 123 </div> 124 <%= f.text_field :price, class:"price-input", id:"item-price", placeholder:"半角数字で記入" %> 125 </div> 126 <div class="price-rate"> 127 <div class="weight-bold-text"> 128 販売時金利 (%) 129 <span class="indispensable">必須</span> 130 </div> 131 <span> 132 <%= f.text_field :rate, class:"rate-input", id:"item-price", placeholder:"半角数字で記入" %> 133 </span> 134 </div> 135 <div class="price-rent"> 136 <div class="weight-bold-text"> 137 家賃 138 <span class="indispensable">必須</span> 139 </div> 140 <span class="sell-yen"></span>円 141 <%= f.text_field :rent, class:"rent-input", id:"item-price", placeholder:"半角数字で記入" %> 142 </div> 143 <div class="price-management"> 144 <div class="management-price-text"> 145 管理費 146 <span class="indispensable">必須</span> 147 </div> 148 <span class="sell-yen"></span>円 149 <%= f.text_field :management, class:"price-input", id:"item-price", placeholder:"半角数字で記入" %> 150 </div> 151 </div> 152 </div> 153 <%# /販売価格 %> 154 155 <%#入居者有無%> 156 <div class = "home-resident"> 157 <div class ="resident"> 158 <div class="weight-bold-text"> 159 入居者 160 </div> 161 <span class="indispensable">必須</span> 162 <div class="field"> 163 <% %w( 入居あり 入居なし).each.with_index do |ingredient,i| %> 164 <label> 165 <%= check_box_tag 'home[ingredients][]', ingredient,false,id:"home_ingredients_#{i}"%> 166 <%= ingredient %> 167 </label> 168 <% end %> 169 </div> 170 </div> 171 </div> 172 <%#/入居者有無%> 173 174 <%# 下部ボタン %> 175 <div class="sell-btn-contents"> 176 <%= f.submit "登録する" ,class:"sell-btn" %> 177 <%=link_to 'もどる', root_path, class:"back-btn" %> 178 </div> 179# /下部ボタン %> 180 </div> 181 <% end %> 182</div> <%
回答1件
あなたの回答
tips
プレビュー