用户和书籍之间的多对多关系,书籍不会出现在视图中

发布于 2024-11-16 02:54:45 字数 1973 浏览 2 评论 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>

书籍控制器

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-11-23 02:54:45

我假设您的示例中的第一个视图实际上是 Books#index 而不是 Home#index 如果这是真的,那么您需要设置变量 @books 到控制器中的某些内容。

使 BooksController 中的索引操作看起来像这样就足够了:

def index
  @books = current_user.books
end

至少如果您使用的是 Rails 3。如果您使用的是早期版本,那么您应该添加对 all 方法的调用,如下所示:

@books = current_user.books.all

I assume that the first view in your example actually is Books#index and not Home#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:

def index
  @books = current_user.books
end

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:

@books = current_user.books.all
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文