ユーザーがログオンしてハイキングコースの詳細をアップロードできる大学向けのアプリを開発しています。
これまでのところすべてが機能しており、各ハイキング トレイルの写真用にネストされたフォームも実装しました。ユーザーはログオンしてハイキングを作成できます。
ユーザーがショー/プロファイルページで作成したすべてのハイキングを表示したいのですが、データベースに関係を設定し、モデルにhas_many
&オプションを設定した場合。belongs_to
また、ネストされたものでこれを実行しようとしましたがaccepts_nested_attributes_for :hikingtrails
、これは機能しません。
ユーザーがハイキングトレイルを作成したときにデータベースをチェックしましたが、テーブルの user_id フィールドが更新されていません。
これに完全に間違った方法でアプローチしているかどうかはわかりませんが、ポリモーフィックな関連付けを見る必要がありますか?
class User < ActiveRecord::Base
attr_accessible :user_name, :email, :password, :password_confirmation, :photos_attributes, :hikingtrails_attributes
has_many :hikingtrails
accepts_nested_attributes_for :hikingtrails, :allow_destroy => :true, :reject_if => :all_blank
class Hikingtrail < ActiveRecord::Base
attr_accessible :description, :name, :looped, :photos_attributes,:directions_attributes, :user_id
has_many :photos
has_many :trails
has_many :directions
belongs_to :user
ユーザー/show.html.erb
<div class="page-header">
<h1>Your Profile</h1>
</div>
<p>
<b>username:</b>
<%= @user.user_name %>
</p>
<p>
<b>email:</b>
<%= @user.email %>
</p>
<h4>Small Photos</h4>
<% @user.photos.each do |photo| %>
<%= image_tag photo.image_url(:thumb).to_s %>
<% end %>
<h4>Hiking Trails</h4>
<% @user.hikingtrails.each do |hk| %>
<%= hk.name %>
<% end %>
<%= link_to "Edit your Profile", edit_user_path(current_user), :class => 'btn btn-mini' %>