###前提・実現したいこと
問い合わせフォームをrequireという名前のモデルで作成し、buildingモデルとアソシエーションして紐付けしたい。
###発生している問題・エラーメッセージ
has_many、belongs_toを各モデルに記入し、require#newでフォームに記入してsubmitしてもエラーは出ないのですがrequireモデルのbuilding_idカラムの値が反映されません。
###該当のソースコード
requires_controller.rb
class RequiresController < InheritedResources::Base def show end def new @require = Require.new end def create @require = Require.new(require_params)
ruby.rb
class Require < ActiveRecord::Base belongs_to :building end
new.html.erb
<% if @require.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@require.errors.count, "error") %> prohibited this require from being saved:</h2> <ul> <% @require.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :tel %><br> <%= f.text_field :tel %> </div> <div class="field"> <%= f.label :email %><br> <%= f.text_field :email %> </div> <div class="field"> <%= f.label :require_date %><br> <%= f.date_select :require_date %> </div> <div class="field"> <%= f.label :require_time %><br> <%= f.time_select :require_time %> </div> <div class="field"> <%= f.label :gender %><br> <%= f.text_field :gender %> </div> <div class="field"> <%= f.label :age %><br> <%= f.text_field :age %> </div> <div class="field"> <%= f.label :occupation %><br> <%= f.text_field :occupation %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
find.html.erb
<div class = "buildingpage-right"> <div class = "requireSelect"> <%= link_to "問い合わせページ", new_require_path(@building) %>
###補足情報(言語/FW/ツール等のバージョンなど)
buildingモデルはuserモデルとも紐付けされています。そちらはcurrent_userが使えるのでusr_idを反映させることができているのですが、、、
初心者ですがどうぞよろしくお願いします。
viewを追記致しました。よろしくお願いいたします。
https://teratail.com/questions/54435 と似ている気が… viewのコードを追記してください
回答1件
あなたの回答
tips
プレビュー