我有一个用户模型和一个书籍模型。用户可以阅读书籍(作为读者),这会在 Readings 模型中创建一个条目:
id | reader_id | book_id
用户也有他们读过的书的清单。这些存储在 Red 模型中(我使用 Red 是因为“read”这个词的现在时和过去时是相同的)模型,看起来与上面的 Reading 模型相同。
现在,当用户正在阅读一本书时,我想显示一个代表完成这本书的按钮。
完成动作在 ReadingsController 中,如下所示:
def finish
@book = current_user.readings.find(params[:id]).book
current_user.stop_reading!(@book)
current_user.make_red! @book
redirect_to :back
end
正如您可能知道的那样,这会获取读数表中记录的 id,将其销毁并在表中创建一条新记录,用于记录书籍为红色。
“完成阅读”按钮的表单助手当前如下所示:
<%= form_for :reading, current_user.readings.find_by_book_id(book.id), :url => { :controller => :readings, :action => "finish" }, :method => :delete do |f| %>
<div class="actions"><%= f.submit button_text %></div>
<% end %>
但是由于某种原因,这会呈现一个带有错误 id 的表单,因为“9781440506604”不是读数表中记录的 id,它是 books 表中记录的 id(准确地说是书的 ISBN-13) .
<form accept-charset="UTF-8" action="/readings/9781440506604/finish" method="post">
</form>
我做错了什么?
编辑添加 reading.rb
class Reading < ActiveRecord::Base
attr_accessible :book_id
# one person reading a new book may cause feed_item creations in multiple users feeds
has_many :feed_items, :as => :event
has_many :comments, :as => :parent, :dependent => :destroy
scope :from_users_followed_by, lambda { |user| followed_by(user) }
# need to pass the class name here because there is no Reader model
belongs_to :reader, :class_name => "User"
belongs_to :book
validates :reader_id, :presence => true
validates :book_id, :presence => true
def self.followed_by(user)
...
end
end
# and user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :avatar, :remember_me, :avatar_url
has_many :readings, :dependent => :destroy,
:foreign_key => "reader_id"
has_many :reads, :through => :readings, :source => :book
has_many :reds, :foreign_key => "reader_id",
:dependent => :destroy
has_many :red, :through => :reds, :source => :book
def reading? book
self.readings.find_by_book_id(book)
end
def read! book
self.readings.create!(:book_id => book.id)
end
def stop_reading! book
self.readings.find_by_book_id(book).destroy
end
def red? book
self.reds.find_by_book_id(book)
end
def make_red! book
unless red? book
self.reds.create!(:book_id => book.id)
end
end
end
顺便说一句,我尝试让一个正在阅读书籍 1 并在控制台中执行 user.readings.find_by_book_id(1) 的用户,它从读数表中返回一条记录。
按照要求
# routes.rb
resources :readings, :only => [:create, :destroy, :show] do
member do
post :create_comment
delete :finish
end
end