以下の3モデル(User, UserBasic, Post) について、データの取得、表示を行いたいです。
Userモデル(親)
Ruby
1class User < ActiveRecord::Base 2 has_one :user_basic, dependent: :destroy, foreign_key: 'id',autosave: true 3 has_one :post, dependent: :destroy, foreign_key: 'id',autosave: true 4 5 accepts_nested_attributes_for :user_basic, :post 6--省略-- 7 8end
Postモデル(子)
Ruby
1class Post < ActiveRecord::Base 2 belongs_to :user, foreign_key: "id", autosave: true 3 4--省略-- 5 6end
UserBasicモデル(子)
Ruby
1class SalonBasic < ActiveRecord::Base 2 belongs_to :salon, autosave: true, foreign_key: 'id' 3 4 before_create do 5 self.id = user.id 6 end 7 8end 9
ここで、UserページとPostページにそれぞれ一覧ページを持たせたいです。
・とあるUserに関連づいたPostを表示するページ(管理用)→Posts#show
・Post一覧を表示するページ →Posts#index
・一覧表示されたPostの詳細を表示するページ → Posts#describe
といった形でアクションを振っています。
以下、show(postモデルで scope :readable_for, ->(user) { user ? full(user) : common } というscopeを使用 )とindexはうまくデータを表示してくれるのですが、findでどのようにデータを渡せばいいのかわかりません。
Ruby
1class PostsController < ApplicationController 2 3 def index 4 @posts = Post.all 5 @user = User.order(:id) 6 @post = @posts.order(:id) 7 end 8 9 def find 10 @post = Post.find(params[:id]) 11 end 12 13 def show 14 @post = Post.readable_for(current_salon).find(params[:id]) 15 end 16 17end
ここで、indexのviewは以下です。
find_posts_pathでfindにリンクさせようとしますが、【Couldn't find User with 'id'=】とエラーが出てしまいます。
Ruby
1<% if @posts.present? %> 2 <% @post.each do |post| %> 3 <div> 4 <section> 5 <%= link_to image_tag("◯◯◯.png"), find_posts_path %> 6 </section> 7 </div> 8 <% end %> 9<% end %>
どのようにすれば、indexで表示させたそれぞれのユーザーの投稿から、その投稿の詳細に飛ばすことができるのでしょうか。
初心者なりに組み立てているので、全然やり方違う!等のご指摘もあるかもしれませんが、よろしくお願いいたします。
追記
Userのshowで管理画面を出し、Postのshowで投稿の個別表示を行うことで解決しました。
ですが、なぜ上記の設定ではうまくいかなかったのかがわからないため、教えていただける方がいましたら是非ご教示お願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/28 09:21