用户和书籍之间的多对多关系,书籍不会出现在视图中
我正在尝试显示用户的书籍列表,但由于某种原因,保存的书籍不会出现在视图上。该应用程序应该允许登录用户保存书籍,然后显示该特定用户保存的书籍列表。这是我的代码..
<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>
书籍控制器
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
用户模型
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%>
I am trying to show a list of user's books,but for some reason the saved books won't appear on the view.The application is supposed to allow a signed in user to save books and then show list of books saved by that particular user. Here is my code..
<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 CONTROLLER
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
Home controller
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
Book model
class Book < ActiveRecord::Base
has_many :book_ownerships
has_many :users,:through => :book_ownerships
end
User model
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
Book ownership model
class BookOwnership < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
Book view
<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%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的示例中的第一个视图实际上是
Books#index
而不是Home#index
如果这是真的,那么您需要设置变量@books
到控制器中的某些内容。使 BooksController 中的索引操作看起来像这样就足够了:
至少如果您使用的是 Rails 3。如果您使用的是早期版本,那么您应该添加对 all 方法的调用,如下所示:
I assume that the first view in your example actually is
Books#index
and notHome#index
and if that is true then you need to set the variable@books
to something in the controller.It should be enough to make the index action in your BooksController look like this:
At least if you are using Rails 3. If you are on an earlier version then you should add the call to the all method like this: