[英]Random Generation of Items from Existing Model
I am fairly new to RoR and trying to get a basic app to work - I have a 'books' model and a 'genre' model.我是 RoR 的新手,并试图让一个基本的应用程序工作 - 我有一个“书籍”模型和一个“流派”模型。 I wish to create a page that randomly generates books of different genre's for a user to select.
我希望创建一个页面,随机生成不同类型的书籍供用户选择。
I have created a 'random_book' controller, but am unsure on how to proceed with the random selection and display.我创建了一个“random_book”控制器,但不确定如何进行随机选择和显示。
Any help/pointers would be appreciated.任何帮助/指针将不胜感激。
Edit:编辑:
Here's the work I've been doing in the random_book model:这是我在 random_book 模型中所做的工作:
" load 'user.rb' " 加载 'user.rb'
class random_book < ActiveRecord::Base class random_book < ActiveRecord::Base
belongs_to :book
belongs_to :genre
def get_random_book
find(:all).sample(5)
end
" ”
Thank you.谢谢你。
Based on discussion基于讨论
4 models 4款
They will look something like this:它们看起来像这样:
class User < ActiveRecord::Base
has_many :user_books
has_many :books, through: :user_books
end
class Genre < ActiveRecord::Base
has_many :books
def fetch_random_books(qty)
#you want to make sure you don't error out by requesting a sample of an empty list of books so check first. The qty argument lets you control the number of books for the search
unless self.books.empty?
self.books.limit(qty).sample
end
end
end
class Book < ActiveRecord::Base
belongs_to :genre
has_many :user_books
end
class UserBook < ActiveRecord::Base
belongs_to :book
end
I would most likely use a different route for the random book section, because it's not very url-friendly to say code-descriptive things like user_book.我很可能会为随机书籍部分使用不同的路线,因为说像 user_book 这样的代码描述性内容对 url 不太友好。
There are 4 things to do有4件事要做
genre_ids
that a user chooses.genre_ids
列表。 Add the route添加路线
post "/random-books", to: "books#random_books", as: :random_books
Create the action in the books_controler在 books_controler 中创建操作
def random_books
if params[:genre_ids]
genres = Genre.where(id: params[:genre_ids])
@books = []
genres.each do |genre|
@books << genre.fetch_random_books(10)
end
else
@books = nil
end
respond_to do |format|
format.html { render action: :index }
end
end
Then create a form that makes a post
request to the index action of the books_controller
-- You can parse the form and update the UserBook model inside that action, and then display list of books all at the same time.然后创建一个表单,向
books_controller
的索引操作发出post
请求——您可以解析表单并更新该操作中的UserBook 模型,然后同时显示所有书籍列表。
<%= form_tag(random_books_path, method: :post) do %>
<ul>
<% Genre.all.each do |genre| %>
<li>
<label class='genre-select'>
<%= check_box_tag 'genre_ids[]', genre.id -%>
<%= genre.name %>
</label>
</li>
<% end %>
</ul>
<%= submit_tag "Fetch Book List"%>
<% end %>
-- The last one I'm sure you can do, it returns a books object list so parse it however works best for you. -- 最后一个我相信你可以做,它返回一个书籍对象列表,所以解析它但是最适合你。 In the controller action you can automatically add the ids for the books to the UserBook model by adding this inside the controller action:
在控制器操作中,您可以通过在控制器操作中添加以下内容来自动将书籍的 id 添加到 UserBook 模型:
@books.each{ |book| book.user_books.create(user: user)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.