这种表格路由有什么问题?

时间:2011-04-06 18:12:47

标签: ruby-on-rails ruby-on-rails-3 rails-routing

我有一个用户模型和一个书模型。用户可以阅读在Readings模型中创建条目的书籍(作为读者):

id | reader_id | book_id

用户还拥有他们已阅读的书籍列表。这些存储在红色中(我使用红色,因为“读取”一词的现在和过去时是相同的)模型,看起来与上面的阅读模型相同。

现在,当用户正在阅读一本书时,我想显示一个表示完成书籍的按钮。

结束动作在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,它是书籍表中记录的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

2 个答案:

答案 0 :(得分:1)

您的to_param模型

中似乎有Reading方法

尝试清楚地调用id:

current_user.readings.find_by_book_id(book.id).id

<强> UPD

  1. 从您的路线中删除:only => [:create, :destroy, :show]
  2. 使用此<%= form_for :reading, current_user.readings.find_by_book_id(book.id), :url => { :controller => :readings, :action => "finish", :id => current_user.readings.find_by_book_id(book.id).id }, :html => {:method => :delete} do |f| %>

答案 1 :(得分:0)

我对rails 3(仍使用rails 2)并不是特别了解,但你不应该将更多信息传递给:url param吗?

这似乎没有提及您要发布到的ID:

:url => { :controller => :readings, :action => "finish" }

不应该更接近这个:

:url => { :controller => :readings, :action => "finish", :id => reading_id }

(假设reading_id代替实际ID)