0
ユーザーの書籍のリストを表示しようとしていますが、何らかの理由で保存した書籍が表示されません。サインインされたユーザーは書籍を保存し、その特定のユーザーによって保存された書籍のリストを表示します。ここに私のコードは..ですユーザーと書籍との関係が多岐にわたるため、書籍は表示されません。
<h1>Home#index</h1>
<%=link_to 'add a book',new_book_path%>
<ul>
<% @books.each do |b|%>
<li><%= b.title %></li>
<li><%= b.author %></li>
<%end%>
</ul>
BOOKコントローラ
class BooksController < ApplicationController
def index
end
def create
@book=current_user.books.build(params[:book])
if @book.save
redirect_to '/home/index'
else
render :action =>'new'
end
end
def new
@book=Book.new
end
end
ホームコントローラ
class HomeController < ApplicationController
#kip_before_filter :authenticate_user!
#efore_filter :homenticate_user!
def index
@books=current_user.books
end
def show
end
def welcome
end
end
ブックモデル
class Book < ActiveRecord::Base
has_many :book_ownerships
has_many :users,:through => :book_ownerships
end
Userモデル
私はあなたの例の最初のビューが実際にBooks#index
ない
Home#index
で、それが本当であるならば、あなたが設定する必要があることを前提としてい
class User < ActiveRecord::Base
has_many :book_ownerships
has_many :books,:through => :book_ownerships
has_many :skills
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,:firstname
end
ブックの所有権モデル
class BookOwnership < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
帳のビュー
<h1>Book#new</h1>
<p>Find me in app/views/book/index.html.erb</p>
<%= form_for(@book) do |f|%>
<%=f.text_field :title%>
<%=f.text_field :author%>
<%=f.submit%>
<%end%>
あなたは – katie
オーケー、その場合には、あなたのホームからのコードを補完することができview..thank #indexコントローラの操作ですか?それがどのように見えるかを確認するのに役立つだろう。 – DanneManne
@katie Danneが上に示したように、home_controllerのインデックスアクションでインスタンス変数を設定する必要があります。私はまた、あなたがDeviseのbefore_filterを介してコントローラを認可していると推測しています。もしそうでなければ、current_userが存在すると仮定しないでください。 –